Jump to content

matching some specific characters in a string


rohant

Recommended Posts

Hi All,

i want to match some specific character set 10 odd characters in a user input string.

i want to find all occurences of the any of such characters.

i am using preg_match_all for this.the string pattern that i am giving is a regular expression having those characters i.e

"ch1|ch2|ch3" ....but i am getting only first occurence of the the char which is there in the regular expression.

where i am goin wrong and how can capture all occurences of those chars.

 

e.g  pattern-  "x|y|z"

 

string to search -  "this is the X string for test Z and Y are at end and are not detected"

 

in above case the character that is matched is only X at 13th position and z and y are not matched

i want to detect presence of all 3 chars. i.e x,y as well as z.

how can i achieve this.

i am a new bee to php so my approach might be incorrect.

Please suggest the correct approach i should follow.

Thanking in adavance,

Regards,

Rohant

 

<?php
$pattern = "/(?im)x|y|z/";  // Using (?im) sets caseless multi line matching
$string = "this is the x string for test Z and Y are at end and are not detected";
preg_match_all($pattern, $string, $matches);

print_r($matches);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.