Reply to PCRE - Negative Lookbehind Assertion problem

Your name:

Reply:


Posted by Jim on 10/29/07 20:49

Hi,

I'm trying to prefix the "src" attribute of all "img" elements with a
given string, $prefix. Here's what I've got:

preg_replace('/\<img(.+?)src="(?<!=http)(.+?)"(.+?)\/>/', '<img
$1src="' . $prefix . '$2"$3/>', $content);

The problem comes in that it always performs the replace, even when
there's an "http" in the source attribute.

Here's the source for a quick test if anyone's interested:

<?php

$prefix = 'http://mydomain.com/';

$content = <<<EOT
<img src="images/test.gif" alt="Test Image" />
<img src="http://shouldnt.be.changed/images/test.gif" alt="Test
Image" />
EOT;

echo preg_replace('/\<img(.+?)src="(?<!=http)(.+?)"(.+?)\/>/', '<img
$1src="' . $prefix . '$2"$3/>', $content);

/*
* First img element's src attribute should be changed, but not the
second's.
*/

?>

Thanks to all,

Jim.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация