mandred Posted May 9, 2009 Share Posted May 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157436-solved-preg_replaceurlhttpi-url-style-not-working-properly/ Share on other sites More sharing options...
nrg_alpha Posted May 9, 2009 Share Posted May 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157436-solved-preg_replaceurlhttpi-url-style-not-working-properly/#findComment-829971 Share on other sites More sharing options...
mandred Posted May 9, 2009 Author Share Posted May 9, 2009 Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/157436-solved-preg_replaceurlhttpi-url-style-not-working-properly/#findComment-829973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.