简体   繁体   中英

PHP, splitting strings by n characters

I'm making my website and I have never come across what is happening here. I am getting all articles from the database and echoing each articles title and a brief 12 characters of the content, this forms a list of the articles to choose from. However, when I use the substr method it doesn't do it correctly.

<?php
                $get_articles = mysql_query("SELECT * FROM `articles` ORDER BY `ID` DESC");
                while($article = mysql_fetch_assoc($get_articles)){
                    $articleID = $article['ID'];
                    $articleTitle = $article['Title'];
                    $articleText = $article['Text'];
                    $articleDate = $article['Date'];

                    $sText = substr($articleText, 0, 12);
                    echo "$sText";
                }
            ?>

this should echo out from my text 'Ahh, Christm' but instead, it give this output 'Ahh, ChristmSeeing if th' which is the first 12 characters and 12 characters from near the end of the article. Help please.

您的代码在每次循环传递时都回显$sText ,没有任何分隔符,因此您可能看到的是数据库中第一个Text字段的substr,紧接着是第二行的第二个Text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM