2011-12-14 36 views
0
<span class="fwb">Harry and David</span> 

<span class="fcg">Business Intelligence Developer/Analyst</span> 
<span class="fcg">Oct 1998 to present</span> 
<span class="fcg">Medford, Oregon</span> 

<span class="fsm fwn fcg">Creative writing</span> 

In my html content 
<span class="fwb"> occured more than 4 times, 
<span class="fcg"> occured more than 10 times, 
<span class="fsm fwn fcg">Creative writing</span> occured more than 5 times. 

使用php reg表達式或DOMDocument()我需要替換所有要附加的符號。使用符號或符號替換HTML標籤值

對於前:

<span class="fwb">Harry and David</span> to be replaced by <span class="fwb">Harry and David$</span> 

<span class="fcg">Business Intelligence Developer/Analyst</span> to be replaced by  <span class="fcg">Business Intelligence Developer/Analyst$</span> 

回答

0

你很難從你的代碼示例,以理解,但我相信你正在尋找一個$標誌追加到每個<span>標籤的結束(?)。使用DomDocument做到這一點:

$str = ' 
<span class="fwb">Harry and David</span> 
<span class="fcg">Business Intelligence Developer/Analyst</span> 
'; 

$dom = new DomDocument(); 
$dom->loadHTML($str); 

$spans = $dom->getElementsByTagName('span'); 
foreach ($spans as $span) { 
    $span->nodeValue = $span->nodeValue . '$'; 
    echo $dom->saveHTML($span) . "\n"; 
} 

你可以像你說的,也可以使用preg_replace()像這樣:

$str = ' 
<span class="fwb">Harry and David</span> 
<span class="fcg">Business Intelligence Developer/Analyst</span> 
'; 

$p = '{(<span[^>]*>)([^<]+)(</span>)}'; 
$r = '$1$2\$$3'; 
echo preg_replace($p, $r, $str); 

此正則表達式例子追加$到每個跨度節點的主題HTML文本結束。

+0

正則表達式工作,並解決了我的問題。 Domdocument顯示錯誤DOMDocument :: saveHTML()期望完全爲0的參數,1在/var/www/fbcrawl/testing.php中給出的第20行。給出一個最好的鏈接學習abt reg表達式 – 2011-12-14 06:32:21