简体   繁体   中英

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

I think I am right in asuming that RegEx can do this job, I'm just not sure how I would do it!

Basically I have a number of links on my website that are in the format of:

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

I need some code that will transform the href value so that it gets outputed in lowercase, but that does not affect the anchor text . Eg:

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

Is this possible? And if so, what would be the code to do this?

you can use preg_replace_callback

something like that

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

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

Using preg_match and strtolower functions

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

Regards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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