2016-02-05 124 views
-4

我搜索了一段腳本,可以看到html標籤和<之間的區別, >放在文本中, 原因是我接收到來自數據庫的文本, 它是由html表格插入的,並且包含文本和html標籤, 文本可以包含<和>,所以標籤 用htmlspecialchars您可以驗證您的文字XHTML, 但你也將更改的標記,如<b>&lt;b&gt;, 所以我需要一個腳本,可以看到這兩個之間的區別...在php中用文本替換小於**(<)**和大於**(>)**的符號

請解決我的問題。

+0

什麼是你當前的字符串?和你想要的字符串是什麼? –

+0

爲什麼不使用正則表達式?我的意思是HTML標記可以包含0個空格,但是當它們不包含時,則必須有其他文本,如href =「/」。 – timmyRS

+0

試試這個: - echo strip_tags($ text); –

回答

0

如果我們可以假設&hellip;

  • HTML標記中<>字符沒有接受有空格,
  • 相同的字符總是通過在條件空格擁抱,並在條件
  • 這些組合存在<,>,< =,> =,! <>

&hellip;這可能是一個解決辦法:

$result = preg_replace('/ ([!]{0,1})(<)([=]{0,1}) /', '$1&lt;$3', $string); 
$result = preg_replace('/ ([!]{0,1})(>)([=]{0,1}) /', '$1&lt;$3', $result); 

這就是正則表達式表示爲<字符:

\   # Match the character 「 」 literally 
(  # Match the regular expression below and capture its match into backreference number 1 
    [!]  # Match the character 「!」 
     {0,1}  # Between zero and one times, as many times as possible, giving back as needed (greedy) 
) 
(  # Match the regular expression below and capture its match into backreference number 2 
    <   # Match the character 「<」 literally 
) 
(  # Match the regular expression below and capture its match into backreference number 3 
    [=]  # Match the character 「=」 
     {0,1}  # Between zero and one times, as many times as possible, giving back as needed (greedy) 
) 
\   # Match the character 「 」 literally 
相關問題