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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.