你的正則表達式創建此輸出:
<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)=("[^"]*")/i
和preg_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" />
你知道樣式表的,對不對? 'img {cursor:pointer;問題解決了。 – bummzack 2012-02-07 07:52:51