开发者

how can i say `from beginning to $string` and `from $string to the end` in regexish for php

I want to remove a part from a text till the given string, and remove another part starting fro开发者_如何学运维m another string in php.

/.+string/i and /anotherstring.+/i didn't work.


/^.*$string/ and /$string.*$/.


'regexish' is a bit more complex than that, so you have to be more precise about what you want

from beginning to the first occurrence of string, but stop on the newline

/^.*?string/

from beginning to the last occurrence of string, stop on the newline

/^.*string/

from beginning to the last occurrence of string, ignoring newlines

/^.*string/s

from beginning to the last occurrence of string, ignoring newlines and case

/^.*string/si

etc etc


you don't have to use too much regex.

$str = "junk junk keyworD1 what i want1 keYWord2 junk junk keyWORD1 what i want2 KEYWORD2";
$s  = preg_split("/keyword1|keyword2/i",$str);
for($i=1;$i<count($s);$i+=2){
 print $s[$i]."\n";
}

from the above , $s[1] will be what you want after keyword1. If there are multiple keyword pairs, the stuff you want are at every 2nd position. ie $s[1],$s[3] and so on.

output

$ php test.php
what i want1
what i want2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜