2012-04-11 40 views
0

使用數據庫顯示的HTML字符的文章:笨character_limiter和strip_tags的

$按行>文章:

<div class="article"> 
As is usual also in Buenos Aires, is held in that city again the official Asterisk Advanced course with the new agenda <a href="http://www.google.com">google page</a> 
</div> 

使用:character_limiter($按行>的文章,160)簡短的文字,但它發生切割超鏈接,HTML代碼生成一個錯誤,我用來解決函數用strip_tags()...但它顯示以下內容:

<div class="article"> 
    As is usual also in Buenos Aires, is held in that city again the official Asterisk Advanced course with the new agendagoogle page... 
    </div> 

正如你所看到的,函數刪除ŧ他超鏈接標籤,但也消除了周圍的空間。

我讀這篇文章:Problem using strip_tags in php

...但使用設置爲2個文本元素正則表達式。我想知道你是否可以使用類似的東西,但有未定義的標籤。我希望你的幫助

回答

0

你不應該有你顯示你的例子是什麼,你有<a>開始標籤和字符串之間的空間。您鏈接到只有將manifesti˚F用戶這個問題正在增加<div>或相似。比如像<b><a><u>等;當某人輸入某些東西時會有自然空間。

你需要做的是以下幾點:

echo character_limiter(strip_tags($row->article), 160); 
  1. strip_tags的第一
  2. 限制字符第二。

不要擔心奇「空間」問題,這將是人爲錯誤的結果,不管怎麼說,如果他們不離開的東西之間的空間像the brown fox<a href="#">jumps over</a> the..

到HTML的替代方法是也使用降價代碼(你在SO上使用什麼來輸入問題/答案)。它是非常清潔恕我直言。在我的網站

+0

,只需使用character_limiter(用strip_tags($按行>的文章),160); 我不認爲這是人爲錯誤(在這種情況下),你在第一個例子中看到,該文本從超鏈接標籤分離。 – csotelo 2012-04-11 14:57:49

+0

奇怪...由於某種原因,這些字符串連接在一起。在wordpress論壇中找到了一個解決方案:preg_replace('/ <[a-zA-Z \ /] [^>] *> /','',$ str);在使用該功能之前。 – csotelo 2012-04-11 15:37:51