我试图在某个点取一个字符串并将其切断(基本上提供了所选文本的预览),但可能存在图像或类似内容(使用BBCode为此),我想知道是否有一种简单的方法来在PHP中做到这一点。将字符串划分为一半而不切割元素
例子:
$content = "blah blah blah such and such [img]imagehere[/img] blah blah";
$preview=unknownfunction($content); //cuts off at approx. 40 chars
//do not want this:
$preview="blah blah blah such and such [img]image";//this is bad because half of image is gone
//want this:
$preview="blah blah blah such and such [img]imagehere[/img]"; //this is good because even though it reached 40 chars, it let it finish the image.
有没有一种简单的方法来做到这一点?或者至少,我可以从预览元素中删除所有标签,但我仍然希望此功能不会切断任何单词。
首先拆下“bbcoded”的内容,然后通过计算余下的字和分裂(MyBB的论坛,例如,工作就像是我最后一次检查) – 2012-02-28 16:38:44
我觉得我经常回答这个问题。 http://stackoverflow.com/a/9202571/383402和其他答案。 – Borealid 2012-02-28 16:40:32
如果您可以获取BB标签列表,您可以使用preg_match_all分割字符串,然后进行计算。否则,您可以使用'['']'字符使用正则表达式,但我不确定它如何解析无法识别的标记。 – inhan 2012-02-28 16:44:05