我試圖在某個點取一個字符串並將其切斷(基本上提供了所選文本的預覽),但可能存在圖像或類似內容(使用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