0

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 . E.g:

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

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

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Mark Jones
  • 89
  • 2
  • 11
  • A regex can't do any transforming. It's a replace function which will do the transforming, regular expressions are only used to help find the right bits to transform – Gareth Dec 03 '10 at 11:27
  • Remember: "example.com/EXAMPLE" is _not_ the same as "example.com/example". You are going to break the link. – KingCrunch Dec 03 '10 at 11:29
  • *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Dec 03 '10 at 11:32
  • Please don't try to parse HTML and transform it with regular expressions. Use a dedicated HTML parser to do the work for you. Look into DOMDocument. – Andy Lester Aug 28 '13 at 23:33

2 Answers2

2

you can use preg_replace_callback

something like that

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

...
preg_replace_callback('/(href="[^"]*")/i' 'replace',$str);
kinnou02
  • 644
  • 8
  • 15
-1

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!

Lobo
  • 4,001
  • 8
  • 37
  • 67