I have a word Hello How are you :chinu i am :good
i want to get the word which contains :
like :chinu
and :good
My code:
<?php
//$string='Hello How are you :chinu i am :good';
//echo strtok($string, ':');
$string='Hello How are you :chinu i am :good';
preg_match('/:([:^]*)/', $string, $matches);
print_r($matches);
?>
Above code i am getting Array ( [0] => : [1] => )
But not getting the exact text. Please help me.
Thanks Chinu
To get all matches you need to use preg_match_all()
. As far as your regular expression goes your negated class is backwards; matching any character of: :
, ^
"zero or more" times and will not match what you expect.
You stated in the comments about the "records" being printed twice, this is because you print the $matches
array itself instead of printing the group index which only displays the match results.
preg_match_all('/:\S+/', $string, $matches);
print_r($matches[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With