tags:

views:

39

answers:

3

Hi,

I want to remove a part from a text till the given string, and remove another part starting from another string in php.

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

+1  A: 

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

Ignacio Vazquez-Abrams
i tried, $codehtmlfir = preg_replace("/^.+html/i", " ", $cahtml, 1);makes nothing
sorry i forgot *
$codehtmlfir = preg_replace("/^.*html/i", " ", $cahtml, 1);doesn't work
@hyilmaz: You have to use them in the same statement I'd assume... `/^.*html/i` will replace the entire string from the beginning until it reaches 'html' somewhere with a space.
animuson
A: 

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
ghostdog74
Ok, and what about minor and major letters. sometimes it can be Keyword or keyWord
then use `preg_split` with `i` modifier
ghostdog74
A: 

'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

stereofrog