Hm... something like this might work:

function forceNewWindow($str) {
	if(!preg_match('/target=["\']_blank["\']/i', $str))
		$str .= ' target="_blank"';

return $str;
}

$pattern = '@(\<a\s+)(href.*)(>.*<img.*>.*</a>)@sUie';
$replace = '"$1" . forceNewWindow("$2") . "$3"';

$html = preg_replace($pattern, $replace, $html);

//$html is the variable taht contains the HTML data to search

    I just want to point out that the "target" attribute is on its way to being deprecated and is only valid in a "loose" DTD; so if you really feel the need to do this (which many users find annoying), be sure that the page is a using a "loose" doctype declaration and not a "strict" one.

      Ah yes, I was going to mention that... I'd read an article somewhere about using rel="external" or something... guess I need to brush up on some W3C standards.

        bradgrafelman wrote:

        ...guess I need to brush up on some W3C standards.

        Eh...why bother? Microsoft doesn't seem to think it's important to. 😉

          <?php
          
          $str = '<a href="http://click.linksynergy.com/fs-bin/click?id=WdDkqYkmPps&offerid=134830.10000191&type=4&subid=0"><IMG alt="Bidz, Inc." border="0" src="http://s3.amazonaws.com/bidzmarketing/091907/300x250_$1_redvelvet.jpg"></a>';
          
          function forceNewWindow($str) {
              if(!preg_match('/target=["\']_blank["\']/i', $str))
                  $str .= ' target="_blank"';
          
          return $str;
          }
          
          $pattern = '@(\<a\s+)(href.*)(>.*<img.*>.*</a>)@sUie';
          $replace = '"$1" . forceNewWindow("$2") . "$3"';
          
          $html = preg_replace($pattern, $replace, $html);
          
          echo $html;
          
          ?>
          

          would this be the way to use this function?

            No. As I said, $html is the variable you supply that as the HTML code to be searched.

              great works wonderful, 1 more question what would I change just to search through a normal link... without an image...

                Thank you very much. You were very helpful. And again thanks to the phpbuilder community!

                  Write a Reply...