user756659
user756659

Reputation: 3512

preg_replace... remove string from string

What I want to do is remove all instances of c_*/ where * could be any digit or character of any length.

$cano = 'www.example.com/example/example2/c_3/example4/';
$cano = preg_replace('c_*/', '', $cano);

I've always been bad with these cause I hardly use them...

Upvotes: 0

Views: 2140

Answers (2)

Skpd
Skpd

Reputation: 670

Try this:

$cano = 'www.example.com/example/example2/c_3/example4/';
$cano = preg_replace('#(?:c_.[^/]/)+#', '', $cano);

Upvotes: 0

David John Welsh
David John Welsh

Reputation: 1569

I think this will do the trick.

$cano = preg_replace('#c_[^/]+/#', '', $cano);

The [^/] matches any character that is not a forward slash, and the + means "one or more" characters.

Upvotes: 3

Related Questions