aalcantara Posted June 29, 2009 Share Posted June 29, 2009 Is there a way to do a condition preg_replace so it replaces the value of 1 to Yes and 0 to No? I know that it is as easy as doing an if statement, but my question refers to using the preg_replace only, to learn and understand the conditional replace. Thanks. Quote Link to comment Share on other sites More sharing options...
.josh Posted June 29, 2009 Share Posted June 29, 2009 preg_replace offers conditions but not really in the way you are looking to do. Basically the way regex conditions work is you can test if a previous capture took part of a match, and if so, continue with this pattern, or else continue with this other pattern. For something like what you are wanting to do, php does not support that. You will have to do multiple regexes inside regular php conditions (if necessary). Like in your case, you could just do 2 separate regexes: $string = preg_replace('~1~','Yes',$string); $string = preg_replace('~0~','No',$string); I think possibly Perl might support integrating conditions inside pattern matching, if that's an option for you. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 29, 2009 Share Posted June 29, 2009 In PCRE, the conditional syntax basically follows along the lines of (?if then | else). So inline with what CV mentioned, it won't serve in the context you seek. The conditional aspect is with regards to captures.. 'if this is captured, then look for that' kind of thing. Example: //$str = 'Two blind mice.'; $str = 'Blind mouse.'; if(preg_match('#(two )?blind (?(1)mice|mouse).#i', $str)){ // either 'Two blind mice.' or 'Blind mouse.' found! (case insensitive) } Bascially, The begining of the pattern looks for two[space] (but optional, as the questionmark afterwards dictates). Since this is a capture, if found will be stored under $1. After blind[space] in the pattern, we arrive at the conditional - (?(1)mice|mouse). It basically says, if the first capture exists (and it will if two[space] if found before blind[space]), at this position in the string, look to see if 'mice' is matched.. if the first capture doesn't exist, look to see if 'mouse' is found instead. Granted, I think one could also use only the if or else component if I'm not mistaken when dealing with conditionals.In your case, you could use str_replace or perhaps even strtr as two additional examples instead of preg_replace (yes, I know, you were interested in regex explicitly). Quote Link to comment Share on other sites More sharing options...
thebadbad Posted July 6, 2009 Share Posted July 6, 2009 Using the e modifier, the replacement is treated as PHP. I know it's not pure regular expressions, but it's done within a single preg_replace() call: <?php $str = '00100100'; echo preg_replace('~[01]~e', "$0 ? 'Yes' : 'No'", $str); //NoNoYesNoNoYesNoNo ?> 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.