Scooby08 Posted February 16, 2009 Share Posted February 16, 2009 I basically have a function like so: <?php function AdjustForEdit($block) { $search[] = "/(['\"])?(templ\/)?images\//m"; $replace[] = "\\1".$SITEURL."/\\2images/"; $ret = preg_replace($search, $replace, $block); return $ret; } ?> I was wondering if anybody could tell me what the numbers 1 and 2 in this line are doing exactly: $replace[] = "\\1".$SITEURL."/\\2images/"; I can't seem to find any documentation on that either because I'm not too familiar with regex and do not know what exactly to search for.. Can anybody recommend any good resources?? Quote Link to comment Share on other sites More sharing options...
.josh Posted February 17, 2009 Share Posted February 17, 2009 they represent the captured data. (['\"]) is the first capture, and (templ\/) is the 2nd capture. The parenthesis mark the capture start and end. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 17, 2009 Share Posted February 17, 2009 Can anybody recommend any good resources?? My personal favorite reference is this book. You can also view phpfreak's resources, as well as their regex tutorial, or sites like this. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted February 17, 2009 Author Share Posted February 17, 2009 Thank you for the info guys.. I have read through some of the documentation and am having troubles figuring out how to do a search for everything except specific words.. For example, I have this line: /href=\"(.*)?\"/ which replaces all href's with whatever I like.. The thing I'm trying to figure out is how would I do the same, except not replace any href's like so: <a href="javascript://"> I'm thinking something like this: [^javascript\:\/\/] but Im not sure how to incorporate that into this line: /href=\"(.*)?\"/ Thanks again.. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 17, 2009 Share Posted February 17, 2009 /href=\"(.*)?\"/ A few things wrong here as is... you don't need to escape " characters in this case.. and avoid .* If you are going to use dot all matching, its generally safer to make it into a lazy quantifier. See this discussion to see why (read from reply 10 onwards). In this case, I would use a negated character class instead. /href="[^"]+"/ The thing I'm trying to figure out is how would I do the same, except not replace any href's like so: <a href="javascript://"> I'm thinking something like this: [^javascript\:\/\/] you are using a negated character class to tell the regex engine not to match either a j, or an a, or a v, etc.. what you need to understand is that a character class just represents one position in the string.. not a sequence such as a capture or non capturing set (...) or (?:...). So if I understand you correctly, you want to replace any href content that is NOT javascript://? You can use negative look ahead assertions. $arr = array('<a href="javascript://" id = "clueless_ID">', '<a href="whatever" class = "I_dunno">', '<a href="javascript://" id = "Rock_Me_Amadeus">'); foreach($arr as &$val){ $val = preg_replace('#(<a.+?href=")(?!javascript://)[^"]+("[^>]*>)#', '\\1\\2', $val); } echo "<pre>".print_r($arr, true); When you run the above script, you'll have to right-click to see the results... any href that has the content javascript:// doesn't get wiped out. Keep hacking away at the material you were reading.. in time, it really starts to fit together. 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.