473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PCRE - Negative Lookbehind Assertion problem

Jim
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.

Oct 29 '07 #1
3 3260
Jim
I just spotted one error, the equals sign after the negative
lookbehind, although that seems to have made little difference.
Updated code follows:

<?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.
*/

?>

Oct 29 '07 #2
On Mon, 29 Oct 2007 22:05:41 +0100, Jim <ji***@yahoo.comwrote:
I just spotted one error, the equals sign after the negative
lookbehind, although that seems to have made little difference.
Updated code follows:

<?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.
*/

?>
Well, taking a guess here, but 'http://' is not preceded by 'http', so it
will match in $2. Lookaheads/behinds without a literal match is often not
a very nice way to go. I'd use the preg_replace_callback or the /e
modifier to let a function examine the match and act apropriately.

--
Rik Wasmus
Oct 29 '07 #3
Jim
Well, taking a guess here, but 'http://' is not preceded by 'http', so it
will match in $2. Lookaheads/behinds without a literal match is often not
a very nice way to go.
Now I'm really confused, I thought that was the idea of the lookbehind
assertion, oh well.
I'd use the preg_replace_callback or the /e
modifier to let a function examine the match and act apropriately.
I'll give that route a try.

Thanks for your help Rik.

Jim.

Oct 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Margaret MacDonald | last post by:
I'm trying to write a filter that will ignore text of the form '\_foo\_' while filtering text of the form '_foo_'. In other words, a backslash is meant to protect against the operation of this...
1
by: Thomas F. O'Connell | last post by:
I've been looking through the negative lookbehind posts and haven't yet found a definitive answer to the question I'm about to ask: Does negative lookbehind have lower precedence than even a...
1
by: mail | last post by:
Hello, I am trying to use regular expressions to scan a subdirectory structure and run sfv and parity file checks on the directory. However, I am having an issue with my current code using...
2
by: brendan | last post by:
Hi ... want to antispam any email address that is written in a page, so long as it is not already contained in a hyperlink ie 'mailto:user@email.com' so am trying to use a negative look behind...
3
by: news | last post by:
Hi all, I am a beginner on 'c' and pcre...and I am on windows 2000 and VC6 with all of the patches, etc. The following program leaks lots of memory and I only make 1 pcre call. I read the...
4
by: DSmith1974 | last post by:
Are lookarounds supported in the boost regex lib? In my VS6 project using boost 1.32.0 I can declare a regex as.. <code_snippet> std::wstring wstrFilename = L"01_BAR08"; boost::wregex...
5
by: vbgunz | last post by:
/* * BEGIN EXAMPLES */ var text = 'A Cats Catalog of Cat Catastrophes and Calamities'; /*** * EXAMPLE 1: negative lookahead assertion logic ***/
2
by: Bart Kastermans | last post by:
I have a file in which I am searching for the letter "i" (actually a bit more general than that, arbitrary regular expressions could occur) as long as it does not occur inside an expression that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.