0

I have problem with this "simple" code:

$img_oryg = '<igm class="aligncenter" src="(oryginal img source)" alt="some text | some text">';
$replace_with = '<div class="ivcs_holder"><igm class="aligncenter" src="(oryginal img source)" alt="some text | some text" /></div>';
$content =  preg_replace("#$img_oryg#s", $replace_with , $contetn );
echo $content;

This is ofcourse a sample code, and everything is coded inside a loop, which overlap each image with divs, and also doeas some other stuff. Problem is in | that can be added to alt-tag of image. If it is present, then code behave strangely and result is not correct.

Anybody does know how to deal with strings like that?

Marc B
  • 356,200
  • 43
  • 426
  • 500
Marcin Bobowski
  • 1,745
  • 2
  • 19
  • 35
  • 1
    For the umptillionth+1 time: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 Do not use regexes to parse HTML. – Marc B Oct 24 '11 at 16:50
  • Ok, now I know, that I should not use regexes to parse HTML - so what should I use to get same result?[? – Marcin Bobowski Oct 24 '11 at 16:54
  • @MarcB strangely enough, some XML/HTML parsers ARE WRITTEN using regexes – Brian Glaz Oct 24 '11 at 17:10

1 Answers1

0

it should be ok as long as you run preg_quote on your regex pattern. the | character has a special meaning in regex, so this is why it would cause problems.

Brian Glaz
  • 15,468
  • 4
  • 37
  • 55
  • Or use str_replace to have same result as someone mentioned in comments :) Oh, and Marc B's reply was rally helpful, couse now I will remember till death ;) – Marcin Bobowski Oct 24 '11 at 17:02