2012-02-07 57 views
2

正則表達式問題正則表達式工作不同

我有正則表達式代碼處理圖形文件。

$view[content] = preg_replace("/(\<img)([^\>]*)(\>)/i", "\\1 name='target_resize_image[]' onclick='image_window(this)' style='cursor:pointer;' \\2 \\3", $view[content]); 

在網頁中,如果沒有「風格‘的HTML代碼,這個代碼工作正常,但如果有,‘風格’,‘樣式’代碼改爲’風格=」光標:指針;「

如果在img處有」style ='...'「,則添加style ='...'」。如果沒有,「風格」代碼應該是「style ='cursor:pointer;'」。

「preg_replace」在img代碼中刪除了「style ='aaaaaa'」「。這應該是「style ='cursor:pointer'」。

碼輸入

<img style="border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" /> 

碼輸出

<img style="cursor:pointer" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" /> 

碼 - 應該是

<img style="cursor:pointer;border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" /> 

任何有用的意見,將不勝感激。

+6

你知道樣式表的,對不對? 'img {cursor:pointer;問題解決了。 – bummzack 2012-02-07 07:52:51

回答

4

你的正則表達式創建此輸出:

<img name='target_resize_image[]' onclick='image_window(this)' style='cursor:pointer;' style="border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif"/> 

請注意,你有-of課程 - 在此字符串兩個樣式屬性。顯然你使用某種HTML驗證,它會通過刪除第二個來自動「修復」這個問題,並且只會給你留下style='cursor:pointer;'

可能提高你的正則表達式。使用/(alt|style|src)=("[^"]*")/ipreg_match_all這樣的模式將允許您提取img標記的屬性,然後對其進行處理並從中構建新的HTML字符串。

無論如何,我不建議使用RegExp的操縱HTML。使用DOM工具更加簡單和健壯。看看Simple HTML DOM Parser。這個簡單的代碼

require 'simple_html_dom.php'; // http://simplehtmldom.sourceforge.net/ 
$html = str_get_html($img); // load HTML as DOM object 
$img = $html->find('img', 0); // find your tag 
$img->style = "cursor:pointer;".$img->style; // manipulate the style attr 
$img->name="target_resize_image[]"; // set other attr's 
$img->onclick="image_window(this)"; 
echo htmlspecialchars($html); // output HTML as string 

給你輸出

<img style="cursor:pointer;border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" name="target_resize_image[]" onclick="image_window(this)" /> 

瞧!它不依賴於你的屬性的順序,用過的引號,空格和換行符。

編輯:WindStory已要求非DOM解決方案

的解決方案取決於您的輸入字符串的給定晶格結構非常多。如果,例如,肯定知道,每一個給定的img已經有一個style屬性,你可以用一個簡單的做替換:

$img = str_replace('style="', 'style="cursor:pointer; ', $img); // add info to the given style-attr 
$img = str_replace('<img', '<img new_attribute="value" ', $img); // add new attrs 

您定標籤擁有較少的保障,您可以使用正則表達式我上述從img標籤

// extract attr's, depends on double quotes 
$Attrs = array(); 
if (preg_match_all('/(alt|style|src)="([^"]*)"/i', $img, $matches, PREG_SET_ORDER)) { 
    foreach ($matches as $match) { 
     $Attrs[$match[1]] = $match[2]; 
    } 
} 

// change/add attr's 
$Attrs['style'] = empty($Attrs['style']) ? 'cursor:pointer' : 'cursor:pointer; '.$Attrs['style']; 
$Attrs['name'] = 'target_resize_image[]'; 
$Attrs['onclick'] = 'image_window(this)'; 

// build new HTML 
$new_img = '<img '; 
foreach ($Attrs as $key => $value) $new_img .= $key.'="'.$value.'" '; 
$new_img .= '/>'; 

看到它在行動here提取的屬性。

希望有所幫助。

編輯2:WindStory已要求更多的容錯解決方案的RegExp

這裏是一個小功能,添加/改變在給定的HTML字符串屬性。它支持單引號或雙引號,但沒有缺失引號,如果單引號括在雙引號中,則不起作用,反之亦然。

function add_attr($html, $name, $value, $append = null) { 
    $attr_pattern = "/\b({$name}=['\"])([^'\"]*)(['\"])/i"; 
    if (preg_match($attr_pattern, $html, $regs)) { 
     if (!is_null($append)) { 
      $value = $regs[2].$append.$value; 
     } 
     $replace = "\\1$value\\3"; 
     $html = preg_replace($attr_pattern, $replace, $html); 
    } else { 
     $tag_pattern = '/<[\w]+\b/i'; 
     $replace = "\\0 $name=\"$value\""; 
     $html = preg_replace($tag_pattern, $replace, $html); 
    } 
    return $html; 
} 

插入HTML的標籤爲$html,定義屬性的$name並定義$value。如果該屬性已經存在,則該值將被替換,否則將被添加。如果您設置了$append,則$value將被附加,$append將被用作級聯標誌。

$img = add_attr($img, 'style', 'cursor:pointer', '; '); 
$img = add_attr($img, 'name', 'target_resize_image[]'); 
$img = add_attr($img, 'onclick', 'image_window(this)'); 
echo htmlspecialchars($img); 

將outout

<img onclick="image_window(this)" name="target_resize_image[]" style="border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none; cursor:pointer;" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" /> 
+0

DerVO /非常感謝您的友好和詳細的答覆。我想有其他方式比dom解析器,這就是爲什麼此代碼查詢記錄的原因。如果可能的話,請讓我知道更簡單的正則表達式。 – WindStory 2012-02-08 07:51:44

+0

我很感激你的寶貴答覆。不過,我想問另外一個問題。 我想同時使用style =「and style =',所以請讓我給這段代碼另外提供一個小竅門。 img); //將信息添加到給定的樣式-attr' 根據您的指示,「style =」「正常工作,但有」style ='「。 等待您的進一步答覆。 – WindStory 2012-02-09 07:18:58

+0

看看我的第二編輯 – DerVO 2012-02-10 13:19:17