roydukkey Posted July 25, 2009 Share Posted July 25, 2009 Ok, I need more help. Hopefully someone understands this. This following code us sucessful, however I need it to do more. It currently finds '$inc (<<path>>)' and returns '<<path>>'. I need the script to disregard the current match when it is between '/*' and '*/'. Thanks you anyone willing to help. $output = preg_replace_callback('/\$inc\\s*\(([^\)]+)\)/is', create_function( '$match', 'global $domain; array_push( $includedFiles, $match[1]); return trim(file_get_contents( $domain.$match[1] ));' ), $output ); Quote Link to comment Share on other sites More sharing options...
Garethp Posted July 25, 2009 Share Posted July 25, 2009 Try using $NewArray = preg_split('/(\/\*|\*\/)/', $string); And then $NewNewArray = []; foreach($NewArray as $k=>$v) { $Tk = $k/2; if(!is_int($Tk)) { continue; } $NewNewArray[] = $v; } And then use run your regex through $NewNewArray or use join on $NewNewArray; This probably isn't the best solution, and it may need some altering, seeing as how I'm not an expert on Regex or anything, but it's worth a try Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 25, 2009 Share Posted July 25, 2009 One possible solution could be to perhaps use preg_split to weed out potential /* comments */ and then check the rest. Since by default, whatever preg_split finds is eliminated (we're not using the PREG_SPLIT_DELIM_CAPTURE flag here), all the array elements containing comments (if they exist) are automatically non-existent. Makes it easier to sift through... Example: $str = 'Text text. /* This is the $inc (<<foo>>) or <<bar>> line coming right up */ Testing $inc (<<bar>>) 1 2 3.'; $arr = preg_split('#/\*.*?\*/#s', $str); foreach($arr as $val){ if(preg_match('#\$inc \(([^)]+)\)#', $val, $match)){ echo $match[1]; } } // Output (in this case, via view source): <<bar>> This of course is a simplified example. If there is plenty of array elements to check, instead of having foreach keep going once a match / capture is made, you could use break instead of the echo statement and then simply echo $match[1] outside the foreach loop afterwards (by perhaps incorporating it into a test to see if it exists): echo (isset($match[1]))? $match[1]: ''; But since this is programming, there are multiple ways to achieve a goal. obviously, the fastest, leanest bug free way would be the best. I can't say this method is it, but it does seem to work. Quote Link to comment Share on other sites More sharing options...
roydukkey Posted July 26, 2009 Author Share Posted July 26, 2009 Though these suggestions are good, there not quite what I need. I need to retain all the input data, comments and everything else. And, then I did you process each match and place it back exactly where it came from, hence the preg_replace_callback. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 26, 2009 Share Posted July 26, 2009 Though these suggestions are good, there not quite what I need. I need to retain all the input data, comments and everything else. This doesn't match your initial explanation. Your initial code also doesn't support this. So I'm not sure why exactly you need to retain this. All you're doing is using a pattern to fetch <<path>>, then array_push it as the last element, then returning a trimmed version of file_get_contents of $domain.match[1] (whatever that may be). So I suspect there is a miscommunication here. Could you not use something along the lines of: $string = 'Text text. /* This is the $inc (<<foo>>) or <<bar>> line coming right up */ Testing $inc (<<bar>>) 1 2 3.'; //obviously, $domain and $includedFiles is assumed already from somewhere else in your script. function returnPath($str){ global $domain, $includedFiles; $arr = preg_split('#(/\*.*?\*/)#s', $str); foreach($arr as $val){ if(strpos($val, "/*") === false){ if(preg_match('#\$inc \(([^)]+)\)#', $val, $match)){ array_push( $includedFiles, $match[1]); return trim(file_get_contents( $domain.$match[1] )); } } } } echo $output = returnPath($string); Obviously, change $string to whatever it is you need (I used that to test as a sample). 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.