play_ Posted May 4, 2007 Share Posted May 4, 2007 from this code: $var1 = "@"; $var2 = "*"; $var3 = "/*"; $string = "Hello world this /* should be ok"; $found = preg_match("/(?:\\$var1|\\$var2|\\$var3)/", $string); if($found) { echo $found; } it returns true, because it find /*. But is there a way to tell which one it found? like, how would i know if it found $var1 or $var2 or $var3? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 7, 2007 Share Posted May 7, 2007 Use three different if(preg_match(... constructs (or even a case block) instead of the "|" in the pattern. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 7, 2007 Share Posted May 7, 2007 <pre> <?php $string = "Hello world this /* should be ok"; $matches = array('@', '*', '/*'); foreach ($matches as &$match) { $match = preg_quote($match, '/'); } $pattern = join('|', $matches); if (preg_match("/($pattern)/", $string, $findings)) { echo $found, '<br>'; print_r($findings); } ?> </pre> Quote Link to comment Share on other sites More sharing options...
play_ Posted May 13, 2007 Author Share Posted May 13, 2007 Thank you Quote Link to comment Share on other sites More sharing options...
play_ Posted May 27, 2007 Author Share Posted May 27, 2007 Hold on not quite. what i'm trying to do is find out which match it found. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 28, 2007 Share Posted May 28, 2007 Can you put your variables in an array? <?php function pq($text) { return preg_quote($text,'/');} $vars = array('*','@','/*'); $string = "Hello world this /* should be ok"; preg_match('/('.implode('|',array_map("pq",$vars)).')/',$string,$match); echo "Found \"$match[1]\"!\n"; ?> If not, maybe something like this: <?php $var1 = "@"; $var2 = "*"; $var3 = "/*"; $string = "Hello world this /* should be ok"; if (is_int(strpos($string,$var1))) { echo "Found \$var1 in \$string."; } elseif (is_int(strpos($string,$var2))) { echo "Found \$var2 in \$string."; } elseif (is_int(strpos($string,$var3))) { echo "Found \$var3 in \$string."; } else { echo "Nothing matched."; } ?> Of course, "*" will match the string since it matches the asterisk in "/*". Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 28, 2007 Share Posted May 28, 2007 Is this what you're after: <?php if (preg_match('/(@|\*|\/\*)/', $string, $match)) { echo "found: $match[1]"; } ?> Good luck. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 29, 2007 Share Posted May 29, 2007 Hold on not quite. what i'm trying to do is find out which match it found. In my example it shows that "/*' was matched. What more do you need? Quote Link to comment Share on other sites More sharing options...
play_ Posted May 30, 2007 Author Share Posted May 30, 2007 effigy, your example outputs: Array ( [0] => /* [1] => /* ) which comes from "print_r($findings);". $found doesn't seem to show anything =/ on php 5.2.1 As for obsidian and wildbug, those worked fine. thanks. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 30, 2007 Share Posted May 30, 2007 Gotcha. $found must have been carried over from copying your code. There is no need for it once preg_match is placed inside an if construct. print_r was a generalization for example; I assumed you'd know how to dig into the array Quote Link to comment Share on other sites More sharing options...
play_ Posted May 31, 2007 Author Share Posted May 31, 2007 sorry :| but thanks all 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.