I have a snippet of html that needs to be cleaned up. All of the u003d's in html tags need to be converted into =, leaving alone all other u003d's (outside of html tags :p ).

Slurping in the whole html code, and using preg_replace, I have no problem replacing the first occurance of u003d in each tag, but am unable to convert multiple occurances in the same tag.

Example html:

<table widthu003d"600" cellpaddingu003d"0" cellspacingu003d"0" bgcoloru003d"#7b849c" borderu003d"0">

I first used str_replace. Works nicely, except the u003d's outside of tags are also changed. Then I used preg_replace, which only replaces the first occurance. Being creative, I thought I could match the tag and use str_replace. No luck. Suggestions are requested. :o

// only first occurance in each tag, of course
$html = preg_replace(array('/(<[^>]*?)u003d([^>]*?'.'>)/'), array('\\1=\\2'), $html);

// this just does not work, not really surprised
$html = preg_replace(array('/(<[^>]*?u003d[^>]*?'.'>)/e'), array('str_replace("u003d","=",\1)'), $html);

I know the array() is not required as is. It's there so I can expand once this works.

    i am not that good with php but i guess you can do it like this also:

    i dont know exactly all the content of the page you want to change but you can replace u003d" to ="

    $string = '<table widthu003d"600" cellpaddingu003d"0" cellspacingu003d"0" bgcoloru003d"#7b849c" borderu003d"0"> u003d u003d testu003d</table>';
    $string = ereg_replace("u003d\"","=\"",$string);

    ofcourse this if in the context of the string wont contain u003d". 😉

    but i am sure that the preg_replace is the perfect way to do it... but I don't really understand it well enough.

    hope it will help you a little

      <pre>
      <?
      $html=<<<EOT
      <table widthu003d"600" cellpaddingu003d"0" cellspacingu003d"0" bgcoloru003d"#7b849c" borderu003d"0">
      EOT;
      echo htmlspecialchars(preg_replace('/(\w)u003d(\W)/','\1=\2',&$html)); 
      ?>
      

        Thank you. The replies certainly gave me a new perspective towards looking for a pattern.

        After trying the solutions, I discovered that not all instances of u003d have a " after it (moraru's solution) and for some strange reason, sql maestro's regex didn't work at all. There must be some hidden characters causing it not to match. Looking for those characters....

        Still at a loss 🙁

          Write a Reply...