如何使用PHP正则表达式转换某些HTML标记属性的内容?

时间:2010-12-03 11:13:06

标签: php regex html-parsing

我认为我认为RegEx能做到这一点是对的,我只是不确定我会怎么做!

基本上我的网站上有许多链接格式为:

<a href="EXAMPLE/Example.html">Example</a>

我需要一些代码来转换href值,使其以小写形式输出,但这不会影响锚文本。 E.g:

<a href="example/example.html">Example</a>

这可能吗?如果是这样,那么代码是什么?

2 个答案:

答案 0 :(得分:2)

你可以使用preg_replace_callback

类似的东西

function replace($match){
    return strtolower($matches[0])
}

...
preg_replace_callback('/(href="[^"]*")/i' 'replace',$str);

答案 1 :(得分:-1)

使用preg_match和strtolower函数

preg_match('/\<a(.*)\>(.*)\<\/a\>/i',$cadena, $a);
$a[1]=strtolower($a[1]);
$cadena = preg_replace('/\<a(.*)\>(.*)\<\/a\>/i',$a[1],$cadena);
echo $cadena;

问候!