regex - PHP - preg_match - assign arbitrary value to a matched element -


assuming have regex:

preg_match('/\b(xbox|xbox360|360|pc|ps3|wii)\b/i' , $string, $matches); 

now, whenever regex match ex. one of 3 xbox methods (xbox|xbox360|360), $matches, should return xbox

is possible continuing work in preg_match() context or should use other method?

thank's in advance.

edited:

im doing this:

$x = array('xbox360','xbox','360'); if( preg_match('/\b(xbox360|xbox|360|pc|ps3)\b/i', $s, $m ) ) {   $t = $m[0]; } if ( in_array($t,$x) ) {   $t = 'xbox'; } 

i'm wondering if there way!

your current code looks ok me, if want bit fancier, can try named subpatterns

preg_match('/\b((?p<xbox>xbox|xbox360|360)|pc|ps3|wii)\b/i' , $string, $matches); $t = isset($matches['xbox']) ? 'xbox' : $matches[0]; 

or preg_replac'ing things before matching:

$string = preg_replace('~\b(xbox|xbox360|360)\b~', 'xbox', $string); preg_match('/\b(xbox|pc|ps3|wii)\b/i' , $string, $matches); 

on big inputs guess method fastest. minor improvement replace in_array hash-based lookup:

$x = array('xbox360' => 1,'xbox' => 1,'360' => 1); if( preg_match('/\b(xbox360|xbox|360|pc|ps3)\b/i', $s, $m ) ) {   $t = $m[0]; } if ( isset($x[$t] ) {   $t = 'xbox'; } 

named subpatterns: see http://www.php.net/manual/en/regexp.reference.subpatterns.php , http://php.net/manual/en/function.preg-match-all.php, example 3


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -