2011-01-11 36 views
0

我有一個很棒的小腳本,它將搜索一個文件並用它們的匹配替換詞替換一個單詞列表。我還找到了一種方法來防止preg_replace替換這些單詞,如果它們出現在錨定標記,img標記或我指定的任何一個標記中。我想創建一個OR語句來指定多個標籤。爲了清楚起見,我想阻止preg_replace替換不僅出現在錨標記中的單詞,而且還會出現錨,鏈接,嵌入,對象,img或span標記中出現的單詞。我試着用'|' OR運算符在代碼中的各個位置都沒有成功。PHP編碼限制來自多個標籤的Preg_replace功能

<?php 
$data = 'somefile.html'; 
$data = file_get_contents($data); 
$search = array ("/(?!(?:[^<]+>|[^>]+<\/a>))\b(red)\b/is","/(?!(?:[^<]+>|[^>]+<\/a>))\b(white)\b/is","/(?!(?:[^<]+>|[^>]+<\/a>))\b(blue)\b/is"); 
$replace = array ('Apple','Potato','Boysenberry'); 
echo preg_replace($search, $replace, $data);?> 
print $data; 
?> 

看第一檢索詞基本上說要尋找「紅色」,而不是裏面:

"/(?!(?:[^<]+>|[^>]+<\/a>))\b(red)\b/is" 

我試圖找出我怎麼能以某種方式添加< \ /鏈接> < \/embed>,< \/object>,< \/img>,以便preg_replace不會替換任何這些標記中的'red'。

+0

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self- contained-tags/1732454#1732454 – dqhendricks 2011-01-11 19:41:48

回答

0

像這樣的事情?:

<?php 
    $file = 'somefile.html'; 
    $data = file_get_contents($file); 
    print "Before:\n$data\n"; 
    $from_to = array("red"=>"Apple", 
        "white"=>"Potato", 
        "blue"=>"Boysenberry"); 
    $tags_to_avoid = array("a", "span", "object", "img", "embed"); 
    $patterns = array(); 
    $replacements = array(); 

    foreach ($from_to as $from=>$to) { 
    $patterns[] = "/(?!(?:[^<]*>|[^>]+<\/(".implode("|",$tags_to_avoid).")>))\b".preg_quote($f 
rom)."\b/is"; 
    $replacements[] = $to; 
    } 

    $data = preg_replace($patterns, $replacements, $data); 

    print "After:\n$data\n"; 
    ?> 

結果:

Before: 
<a href="red.html">red</a> 
<span class="blue">red</span> 
blue<div class="blue">white</div> 
<div class="blue">red</div> 

After: 
<a href="red.html">red</a> 
<span class="blue">red</span> 
Boysenberry<div class="blue">Potato</div> 
<div class="blue">Apple</div>