473,795 Members | 3,151 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.cha nged/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 3317
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.cha nged/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.co mwrote:
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.cha nged/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_ca llback 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_ca llback 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
3247
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 particular filter. To detect the leading backslash once I find the underscore, I've been trying to use a 'negative lookbehind' in preg_replace. It seems a perfect use for it: if we find an under but it was preceded by a backslash, don't...
1
6152
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 non-greedy wildcard (*) in the Perl regular expression engine? The reason I ask is the following scenario: I am try to match a pattern B as long as it is not preceded by a pattern A in a given string, regardless of what occurs between B and
1
2254
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 regular expressions to find par2 files. Multiple par2 files are created for an archive in the following format: test.par2 test.vol000+01.par2 test.vol000+03.par2
2
2378
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 but I can't get it to work preg_replace('/(?<!mailto)((+)@(+))/i', "<script>document.write('<a href=\'mailto:'+'\\1'+'@'+'\\2'+'\'>'+'\\1'+'@'+'\\2'+'</a>');</script>",$st ring );
3
2000
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 docs, but maybe I missed some cleanup or other calls??? Or maybe I messed up passing strings around???
4
4404
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 regxCarFile( L"(?=BAR)BAR{2}" ); bRet = boost::regex_search( wstrFilename, m, regxCarFile, boost::match_default ); if( true == bRet )
5
2316
by: vbgunz | last post by:
/* * BEGIN EXAMPLES */ var text = 'A Cats Catalog of Cat Catastrophes and Calamities'; /*** * EXAMPLE 1: negative lookahead assertion logic ***/
2
1516
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 matches \\.+?\b (something started by a backslash and including the word that follows). More concrete example, I have the string "\sin(i)" and I want to match the argument, but not the i in \sin. Can this be achieved by combining the regular...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.