ToonMariner Posted October 31, 2006 Share Posted October 31, 2006 Hi peeps,OK i have a series of statements in a css file like so[code]#nav li#navintroduction a{ background: transparent url(../images/nav/red.gif) no-repeat 0 0;}#nav li#navproducts a{ background: transparent url(../images/nav/lightblue.gif) no-repeat 0 0;}#nav li#navbargain_selection a{ background: transparent url(../images/nav/orange.gif) no-repeat 0 0;}#nav li#navspecials a{ background: transparent url(../images/nav/brown.gif) no-repeat 0 0;}#nav li#navcontact a{ background: transparent url(../images/nav/purple.gif) no-repeat 0 0;}[/code]WITH THE NEW LINESWhat I need to do is alter the image name (just the first part) based on id of the li element.This is what I am trying but no joy as yet (i think i am pretty close though ;))[code]<?php$str = "#nav li#navintroduction a{ background: transparent url(../images/nav/red.gif) no-repeat 0 0;}#nav li#navproducts a{ background: transparent url(../images/nav/lightblue.gif) no-repeat 0 0;}#nav li#navbargain_selection a{ background: transparent url(../images/nav/orange.gif) no-repeat 0 0;}#nav li#navspecials a{ background: transparent url(../images/nav/brown.gif) no-repeat 0 0;}#nav li#navcontact a{ background: transparent url(../images/nav/purple.gif) no-repeat 0 0;}";$styles = array ( 1 => 'red' , 2 => 'lightblue' , 3 => 'orange' , 4 => 'brown' , 5 => 'purple' );$newsty = 4;$id = 'products';$filestr = preg_replace('/li#nav' . $new . ' a(.*)\/nav\/(.*)\.gif/s','li#nav' . $new . ' a$1/nav/' . $styles[$newsty] . '.gif',$str);?>[/code]So basically I just want to change the initial part of the file name BUT I think preg_replace is greedy (is it?) so i am not getting anything lie what i expect.Hopefully from the code you will be able to see what I am trying to do, and even more hopefully point me in teh right direction.Cheers people. Quote Link to comment Share on other sites More sharing options...
effigy Posted October 31, 2006 Share Posted October 31, 2006 What is[tt] $new[/tt]? Why are you using an associative array? I'm still confused at what your desired result is--can you show an example? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted October 31, 2006 Author Share Posted October 31, 2006 Sorry. a typo shoudl be $id.So the regex is...[code]<?php$filestr = preg_replace('/li#nav' . $id . ' a(.*)\/nav\/(.*)\.gif/s','li#nav' . $id . ' a$1/nav/' . $styles[$newsty] . '.gif',$str);?>[/code](this is going into a function so $id will chage)so With[code]<?php<?php$str = "#nav li#navintroduction a{ background: transparent url(../images/nav/red.gif) no-repeat 0 0;}#nav li#navproducts a{ background: transparent url(../images/nav/lightblue.gif) no-repeat 0 0;}#nav li#navbargain_selection a{ background: transparent url(../images/nav/orange.gif) no-repeat 0 0;}#nav li#navspecials a{ background: transparent url(../images/nav/brown.gif) no-repeat 0 0;}#nav li#navcontact a{ background: transparent url(../images/nav/purple.gif) no-repeat 0 0;}";$styles = array ( 1 => 'red' , 2 => 'lightblue' , 3 => 'orange' , 4 => 'brown' , 5 => 'purple' );$newsty = 4;$id = 'products';$filestr = preg_replace('/li#nav' . $id . ' a(.*)\/nav\/(.*)\.gif/s','li#nav' . $id . ' a$1/nav/' . $styles[$newsty] . '.gif',$str);?>[/code]So with $id = "products" and $newsty = 4 I would like get#nav li#navintroduction a{ background: transparent url(../images/nav/red.gif) no-repeat 0 0;}#nav li#navproducts a{ background: transparent url(../images/nav/[color=blue]brown[/color].gif) no-repeat 0 0;}#nav li#navbargain_selection a{ background: transparent url(../images/nav/orange.gif) no-repeat 0 0;}#nav li#navspecials a{ background: transparent url(../images/nav/brown.gif) no-repeat 0 0;}#nav li#navcontact a{ background: transparent url(../images/nav/purple.gif) no-repeat 0 0;} Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted October 31, 2006 Author Share Posted October 31, 2006 I think I need to do a negative lookahead, something like..[code]filestr = preg_replace('/li#nav' . $id . ' a(.*)\/nav\/(?!(\.gif))\.gif/s','li#nav' . $id . ' a$1/nav/' . $styles[$newsty] . '.gif',$str);[/code]But how do I get that negative lookahead to include linebreaks? Quote Link to comment Share on other sites More sharing options...
effigy Posted October 31, 2006 Share Posted October 31, 2006 [tt]preg_replace('%(li#nav' . $id . ' a.+?/nav/).+?(\.gif)%s', '$1' . $styles[$newsty] . '$2',$str)[/tt] Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 1, 2006 Author Share Posted November 1, 2006 Brilliant!!!!Thank you VERY much. Quote Link to comment 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.