[英]Split variable content into multiple paragraphs
Hey there, I have this little php code: 嘿,我有这个小php代码:
<p class="category_text"><? echo $category_text; ?></p>
I waht to split the $category_text and get something like this: 我将拆分$ category_text并得到如下内容:
This is sentence 1 of category_text 这是category_text的句子1
This is sentence 2 of category_text 这是category_text的句子2
and so on... 等等...
$category_text has about 300 words and lets say 6 sentences. $ category_text大约有300个单词,可以说6个句子。 How could I split the text in multiple paragraphs (delimited by the stop sings ".")
如何将文本拆分为多个段落(以停止符“。”分隔)
Thank you very much! 非常感谢你!
echo '<p class="category_text">'
. implode('</p><p class="category_text">', explode('.',$string))
.'</p>';
You can just replace the "." 您可以只替换“。” by the tag "
通过标签“
": “:
<p class="category_text"><? echo str_replace('.', '.<br />', $category_text); ?></p>
It's not a perfect solution! 这不是一个完美的解决方案! But if you text is simple enough this little trick should work.
但是,如果您输入的文字足够简单,则此小技巧应该会起作用。
For example if you have a line with 3 dots: 例如,如果您有一条包含3个点的线:
$category_text = "Ok...";
It will show up like that: 它将显示为:
OK.
.
.
Also if your sentences finish by "?" 另外,如果您的句子以“?”结尾 or "!"
要么 ”!” you can also use that:
您还可以使用:
<p class="category_text"><? echo str_replace(array('.', '!', '?'), array('.<br />', '!<br />', '?<br />'), $category_text); ?></p>
PS: My solution will create one paragraph " PS:我的解决方案将创建一个段落“
" but with multiple line break ”,但有多个换行符
Try creating an array, and then output the lines one by one. 尝试创建一个数组,然后逐行输出行。 A sentence ending in ... would still be recognized as still ends in ". ".
以...结尾的句子仍将以“。”结尾。
$sentences = explode('. ', $category_text)
foreach($sentences as $val)
{
echo $val . ".<br /><br />";
}
You want to split a text into sentences, which is not trivial - using explode(".", $string)
does often not give good results. 您想将文本拆分为一些句子,这并非易事-使用
explode(".", $string)
通常不会产生良好的结果。
Search Stackoverflow for "php split sentence", or directly try the solution to PHP: Parse document / text into sentences : 在Stackoverflow上搜索“ PHP拆分句子”,或直接尝试解决PHP的方法:将文档/文本解析为句子 :
http://www.zubrag.com/scripts/text-splitter.php
http://www.zubrag.com/scripts/text-splitter.php
Once you have an array with sentences, use 一旦有了包含句子的数组,请使用
echo '<p>' . implode('</p><p>', $sentences) . '</p>';
to echo them out. 呼应他们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.