简体   繁体   English

PHP,以n个字符分割字符串

[英]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. 我从数据库中获取所有文章,并回显每个文章标题和内容的简短12个字符,这形成了可供选择的文章列表。 However, when I use the substr method it doesn't do it correctly. 但是,当我使用substr方法时,它无法正确执行。

<?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. 这应该从我的文本“ Ahh,Christm”中回显出来,但是,它却给出了输出“ Ahh,ChristmSee if”,这是文章末尾的前12个字符和12个字符。 Help please. 请帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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