Jump to content

[SOLVED] preg_replace('/url\([^http]/i', 'url(../', $style) not working properly.


mandred

Recommended Posts

Trying to replace all instances of url( with url(../ IF http does not come directly after it.

 

My regex:

 

<?php
$style = preg_replace('/url\([^http]/i', 'url(../', $style);

echo $style; 
?>

 

Seems to work, except for the fact that url(images/logo.gif) becomes url(../mages/logo.gif). The I in IMAGES is gone.

Perhaps something along the lines of

 

$str = 'Some text url(images/logo.gif) some more text url(http://www.whatever.com/images2/logo2,jpeg) yet more text url(images3/logo3.gif)';
$str = preg_replace('#(url\((?!http))#', '$1../', $str);

?

 

On a side note.. when you use [^http], what is important to understand is that the character class [...] looks for a single character.. so in this case, what you are saying is, at the current position in the string being checked, the current character cannot be an h, nor a t, nor a t, nor a p. What I've done was use a negative look ahead assertion (?!http).\  So this means, if url( is found, check to see if the following characters are not http. Hope I explained this properly.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.