Php 正则表达式的工作原理不同

Php 正则表达式的工作原理不同,php,regex,Php,Regex,正则表达式问题 我有处理图形文件的正则表达式代码 $view[content] = preg_replace("/(\<img )([^\>]*)(\>)/i", "\\1 name='target_resize_image[]' onclick='image_window(this)' style='cursor:pointer;' \\2 \\3", $view[content]); 在网页上,如果html代码中没有“样式”,则此代码可以正常工作。但是如果有样式,样式代码

正则表达式问题

我有处理图形文件的正则表达式代码

$view[content] = preg_replace("/(\<img )([^\>]*)(\>)/i", "\\1 name='target_resize_image[]' onclick='image_window(this)' style='cursor:pointer;' \\2 \\3", $view[content]);
在网页上,如果html代码中没有“样式”,则此代码可以正常工作。但是如果有样式,样式代码将更改为style='cursor:pointer

I wnat,如果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" /> 

任何有帮助的评论都将不胜感激。

您的regexp将创建以下输出:

<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" /> 
提供输出

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
在给定标记上的保证较少的情况下,可以使用上面提到的RegExp从img标记中提取属性

会出局的

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;
}

您的regexp将创建以下输出:

<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" /> 
提供输出

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
在给定标记上的保证较少的情况下,可以使用上面提到的RegExp从img标记中提取属性

会出局的

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;
}

你知道样式表,对吗?img{cursor:pointer;}。。。问题解决了。你知道样式表,对吗?img{cursor:pointer;}。。。问题解决了。DerVO/非常感谢您的友好和详细的回复。我希望使用dom解析器以外的其他方法,这就是为什么这段代码可以用于查询记录。如果可能的话,请让我知道更简单的正则表达式。谢谢你宝贵的回复。不过,我想再问一个问题。我想同时满足style=和style=,所以请让我有关于这段代码的另一个技巧$img=str_替换'style=','style=cursor:pointer;'$img;//根据您的说明向给定的样式属性添加信息,style=可以正常工作,但也有style='。等待您的进一步答复。DerVO/我感谢您的友好回答。您可以通过单击复选标记接受他的解决方案来表示感谢;DerVO/非常感谢您友好而详细的回复。我希望使用dom解析器以外的其他方法,这就是为什么这段代码可以用于查询记录。如果可能的话,请让我知道更简单的正则表达式。谢谢你宝贵的回复。不过,我想再问一个问题。我想同时满足style=和style=,所以请让我有关于这段代码的另一个技巧$img=str_替换'style=','style=cursor:pointer;'$img;//根据您的说明向给定的样式属性添加信息,style=可以正常工作,但也有style='。等待您的进一步答复。DerVO/我感谢您的友好回答。您可以通过单击复选标记接受他的解决方案来表示感谢;
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;
}
$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);
<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" />