2012-11-28 14 views
0

我在使用strpos時遇到了問題。如果我搜索<br />它會找到它。如果我搜索<br /><br /><br />之間有或沒有空格,它不會和使用htmlspecialchars我可以告訴字符串已滿。php strpos在搜索時不工作<br /><br /> after nl2br()

<?php 
    $picArray = glob('projectData/' . $data['folder'] . '/*.jpg',GLOB_BRACE); 

    $text = nl2br($data['definition']).'<br />'; 

    $cutP = 0; 
    foreach($picArray AS $insert) { 

     if(strpos($text,'<br /> <br /> <br />',$cutP) !== FALSE){ 
      $cutP = strpos($text,'<br /> <br /> <br />',$cutP)+6; 
      echo $cutP.'_'; 
      $str_to_insert = '<img class="inTextImg" title="int" src="'.$insert.'" />'; 
      $text = substr($text, 0, $cutP) . $str_to_insert . substr($text, $cutP); 
     } 
     else { 
      echo 'haha'; 
      $text .= '<img class="inTextImg" title="outText!" src="'.$insert.'" />'; 
     } 
    } 
    ?> 

感謝您的意見。

+0

echo $ text;'的輸出是什麼? –

+0

也許你認爲'<'實際上是'<'? – ficuscr

+0

你能把這個問題分解成簡單的腳本嗎?沒有任何引用你的fodler結構等只是舉例說明它何時失敗。一般來說,最好使用正則表達式來完成這類任務。 –

回答

2

這是因爲nl2br保持原有換行字符的地方,就在'<br />'後。您需要在字符串中包含換行符以進行搜索。因爲可以有幾種不同的模式,所以使用正則表達式最容易匹配:

$text = preg_replace('/(?:<br \/>\r?\n?){3}/', $str_to_insert, $text); 
+0

立即嘗試 –

+0

+1的位置,以解釋爲什麼'strpos'無法與字符串匹配。 – ficuscr

+0

謝謝我需要保留
,所以我做了$ text = str_replace(「\ n」,'',$ text); $ text = str_replace(「\ r」,'',$ text); –

0

您是否嘗試過使用preg_match()?

if(preg_match("\(<br />)+\",$text) > 0){ 
    // code 
} 

不上正則表達式100%,但你會想要一個檢查一個或多個標籤BR