2012-01-24 50 views
0

當我在下面的第二個內容片段上運行此腳本時,它會通過(我得到「內部回調」回聲)。但是,當我在第一個內容片段上運行它時,即使認爲我得到了第一個回聲,我也從來沒有在p_callback()中獲取回顯。爲什麼preg_replace_callback不會觸發?

我猜測,內容中有某些內容會跳出preg_replace_callback。你會怎麼建議導致這個問題?

function ad_insert($content){ 
    if(substr_count(strtolower($content), '</p>') < get_option('ad_insert')) 
    { 
     return $content .= '<p class="endContent">' . get_ads($ad_insert=1) . '</p>'; 
    } 
    else 
    { 
    echo "inside else"; 
    $replaced_content = preg_replace_callback('#(<p>.*?</p>)#', 'p_callback', $content); 
    } 
    return $replaced_content; 
} 

function p_callback($matches) 
{ 
    echo "inside callback";die; 
    static $count = 0; 
    $ret = $matches[1]; 
    $pCount = get_option('cb2_ad_insert'); 

    if (++$count == $pCount){ 
     $ret .= '<p class="insertContent">' . ce4_get_ads($ad_insert=1) . '</p>'; 
    } 

    return $ret; 
} 

第一個內容片斷失敗:

<div style="margin: 10px 0;"> 
    <a href="test.jpg" target="_blank" rel="nofollow"> 
     <img style="float: left; margin: 10px; max-width: 25%;" title="test" src="test.jpg" alt="test" /> 
    </a> 
    <p style="float: left; width: 70%;"> 
     <a href="test" target="_blank" rel="nofollow"> 
      <img title="PDF file" src="test.png" alt="PDF file" /> 
      <span style="font-weight: bold; text-transform: capitalize;">Test ...</span> 
     </a> 
     <span>test <strong>test</strong> test? test, test, i.e., </span> 
     <a href="test.pdf" target="_blank" rel="nofollow"> ... Get Content Here</a> 
    </p> 
</div> 

第二內容片段經過:

<div style="margin: 10px 0;"> 
    <p>first paragraph</p> 
    <p>second paragraph</p> 
</div> 
+0

只有多數接近的選票顯示原因。我的問題是「這個問題是由於一個無法複製的問題或一個簡單的印刷錯誤造成的。雖然類似的問題可能在這裏討論,但這個問題的解決方式不太可能有助於未來的讀者。」 –

回答

1

你的第一個片段不包含<p>...</p>

你的段落標記有屬性。

+0

謝謝,看起來像我需要改變匹配正則表達式 – RegEdit

0

使用s (PCRE_DOTALL)修改:

$replaced_content = preg_replace_callback('#(<p.*?</p>)#s', 'p_callback', $content); 
                ____^ 
相關問題