繁体   English   中英

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

[英]How can I use a PHP regex to transform the contents of certain HTML tag attributes?

我认为RegEx可以完成这项工作是正确的,但我不确定该怎么做!

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

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

我需要一些代码来转换href值,以便将其转换为小写形式,但这不会影响锚文本。 例如:

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

这可能吗? 如果是这样,执行此操作的代码将是什么?

您可以使用preg_replace_callback

这样的东西

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

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

使用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;

问候!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM