Forum Moderators: coopster

Message Too Old, No Replies

preg replace

Simple example please.

         

gosman

11:50 pm on Nov 19, 2007 (gmt 0)

10+ Year Member



Hopefully someone can help.

I'm a complete newbie to PHP I'm trying to find a preg_replace example using wildcards that makes sense.

Here's what I'm trying to acheive.

I have a string of text formatted as follows.

someurl-c*.htm

what I want to replace is the -c*.htm so I'm left with just

someurl

c* is a wildcard could be c1, c2, c1000, etc..

I have tried to figure this out before posting but all examples I can find are a bit complex. Maybe it's just late :-)

cameraman

1:52 am on Nov 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$subject="some-url-csldkfj.htm";
$subject = preg_replace('/(-c)(.*)/','',$subject);

(-c) says look for hyphen followed by the letter 'c'.
(.*) says 'everything after that'.

Whatever's not included in the match won't be replaced, and you're replacing the matched parts with nothing, so the result:
$subject="some-url"

gosman

7:54 am on Nov 20, 2007 (gmt 0)

10+ Year Member



cameraman that's what I call a peefect example for a newbie ;-)

Thnank you.