How to iterate loop inside a string searching for any word aftera fixed keyword?
Suppose I have a sting as "PHP Paddy,PHP Pranav,PHP Parth", now i have a count as 3,now how should I iterate loop in the string aiming on string after "PHP " to display the all the names?
Alright This is the string "BEGIN IF (NEW.name != OLD.name) THEN INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.name, newvalue = NEW.name, field = "name"; END IF; IF (NEW.alias != OLD.alias) THEN INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.alias, newvalue = NEW.alias, field = "alias"; END IF; END
" in which i am searching the particular word after开发者_如何学Go " IF (NEW.
", and after that particualar others strings should not b displayed, hence whenever in a loop it finds " IF (NEW.
" I musr get a word just next to it. and in this way an array should b ready for to use.
Or you can (more or less) "combine" the explode() and substr() thingy into one call of preg_match_all()
$pattern = '/(?<=PHP )[^,]+/';
$input = "PHP Paddy,PHP Pranav,PHP Parth";
preg_match_all($pattern, $input, $captures);
foreach( $captures[0] as $e ) {
echo $e, "\n";
}
prints
Paddy
Pranav
Parth
Use explode
and substr
:
$str = "PHP Paddy,PHP Pranav,PHP Parth";
$ary = explode(',', $str);
foreach($ary as $str){
echo substr($str, 4);
}
$str = "PHP Paddy Paddy,PHP Pranav,PHP Parth";
$s = array_filter(preg_split("/,|PHP/",$str));
print_r($s);
you can do:
$str = "PHP Paddy,PHP Pranav,PHP Parth";
foreach(explode(',',$str) as $p) {
echo substr($p,4)."\n";
}
- First we split the string on comma to
get an array with
3
elements - Next we iterate through the array and
print each element skipping the first
4
char.
Also can use strstr
$str = "PHP Paddy,PHP Pranav,PHP Parth";
foreach(explode(',',$str) as $st) {
echo strstr($st,' ')."\n";
}
精彩评论