Forum Moderators: coopster

Message Too Old, No Replies

triming a string

remove specfic word when it appears as first word in string

         

santapaws

7:09 pm on Feb 15, 2012 (gmt 0)

10+ Year Member



Trying to work out how to remove a specfic word (say foo for example) when it is the first word in a string. Any pointers? tx.

penders

7:53 pm on Feb 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Assuming the word is always followed by a white-space (\s) character then...

$str = preg_replace('/^foo\s/','',$str);


^ is a position marker indicating the start of the string.

santapaws

10:43 pm on Feb 15, 2012 (gmt 0)

10+ Year Member



thanks works great, i wonder how i would make this case insensitive? :)

penders

11:08 pm on Feb 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$str = preg_replace('/^foo\s/i','',$str);


case isensitive (pattern modifier)

santapaws

11:14 pm on Feb 15, 2012 (gmt 0)

10+ Year Member



excllent. Thanks.