BloodyMind Posted November 18, 2010 Share Posted November 18, 2010 I'm building a wordpress plugin, so I want the user to put an image list, and I later put on a jquery plugin to expect this <ul> <li><a><img attr1='val1' class="a"></a></li> <li><a><img attr1='val1' ></a></li> </ul> and matches this structure, if it matched, I would replace the class attribute if it exists with myclassname and if it doesn't it adds it to the img tag consider this input: <html> <head><title>Testing RegEx</title> <style type='text/css'> .p{ font-color:red; } </style> </head> <body> <p> Regex is <b> awesome </b>.peep, creep</p> <p> Another paragraph <ul> <li><a href='#' >dzzd</a></li> <li><a href='#' ><img alt='dzzd' /></a></li> <li><a href='#' ><img alt='dzzd' class='omar' /></a></li> <li><a href='#' ><img alt='dzzd' class='omarr' /></a></li> </ul> </p> <b>aweful</b> </body> </html> What I have done so far is the following $exp="#<ul>.*?(<li>(<a(.*?[^>])>)(?>(.*?)</li>))+.*?</ul>#Uis"; $replaced = preg_replace_callback($exp,addClass,$str); function addClass($groups){ $patt = '#<img(.*)(?!class)(.*)/>#Uis'; return preg_replace($patt,'<img $2 $3 class="myclassname"',$groups[0]); } I'm trying to match anything in the tag that is not class='anything' then match class='anything' then match anything after class='anything' but I can't get it to work, I've read that the negative look-ahead assertions will help, but I'm trying to learn how to use it so any help will be really appreciated Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted November 18, 2010 Share Posted November 18, 2010 This would be so much simpler if you just did it with jQuery to add the class, or better still just give the <ul> an id and then use $('#ul_id_here > li > a > img') to select all of the images in the ul. You can then remove all classes from them, and replace with the ones you want, or just use the selected images directly with that Quote Link to comment Share on other sites More sharing options...
BloodyMind Posted November 18, 2010 Author Share Posted November 18, 2010 you are right, and I've been for like 4 days trying to get it to work, but I don't wanna waste all the time, on nothing, I may do your solution for now, but my solution will be faster since, you don't have to manipulate the html each time it is loaded, you will modify it for once and then it will work normally all the way. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted November 18, 2010 Share Posted November 18, 2010 You could also just remove the class from the images with one regex, then add it to all images with another This will remove all classes from the img tags $result = preg_replace('/(<img\b[^>]*) class=\'[^\']+\'([^>]*>)/m', '$1$2', $subject); Then you just replace all images with a class. You will probably need to extend the above a little to have the tags around the img tags too, but that works for removing all image classes Quote Link to comment Share on other sites More sharing options...
BloodyMind Posted November 18, 2010 Author Share Posted November 18, 2010 Thanks so much, I've managed to run the main match 2 times, one to remove the class, and one to add the correct one You really saved alot of my nerves thanks so much Quote Link to comment Share on other sites More sharing options...
BloodyMind Posted November 18, 2010 Author Share Posted November 18, 2010 Here is the final code so everyone who come accross this problem benefits from it: $str = " <html> <head><title>Testing RegEx</title> <style type='text/css'> .p{ font-color:red; } </style> </head> <body> <p> Regex is <b> awesome </b>.peep, creep</p> <p> Another paragraph <ul> <li><a href='#' ><img alt='dzzd' /></a></li> <li><a href='#' ><img alt='dzzd' class='omar' /></a></li> <li><a href='#' ><img alt='dzzd' class='omarr' /></a></li> </ul> </p> <b>aweful</b> </body> </html> "; // match the ul that has li>a>img $exp="#<ul>.*?(<li>(<a(.*?[^>])>)(?>(.*?)</li>))+.*?</ul>#Uis"; $replaced = preg_replace_callback($exp,adjustImages,$str); // remove the list: ul li function removeList($groups){ $groups[0] = str_replace(array('<li>','</li>','<ul>','</ul>'),'',$groups[0]); return $groups[0]; } // add the desired class to the img tags function addClass($groups){ return preg_replace('/(<img\b[^>]*) /Uis', '$1 class="thumb" ', $groups[0]); } // remove the existing classes function removeClasses($groups){ return preg_replace('/(<img\b[^>]*) class=\'[^\']+\'([^>]*>)/Uis', '$1$2', $groups[0]); } /** * Remove any classes from img tags, * add the desired class to the images * remove the lists */ function adjustImages($groups){ $groups[0] = addClass($groups); $groups[0] = removeClasses($groups); $groups[0] = removeList($groups); return $groups[0]; } Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted November 18, 2010 Share Posted November 18, 2010 No problem 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.