notforever
notforever

Reputation: 589

preg_replace to change image path

I need to replace in a php file some text from DB with preg_replace. The string to replace is:

images/

and change it to

cms/images/

I know it's simple but I just can't understand this syntax.

Upvotes: 0

Views: 2251

Answers (2)

Ben Swinburne
Ben Swinburne

Reputation: 26467

Since the OP requested a solution using preg_replace() here's one. Otherwise the solution of @genesis is more than suitable.

$new_string = preg_replace('#images/#i', 'cms/images/', $string);

The above simply replaces images/ with cms/images/ in $string. The # symbols are delimiters which separate the expression from the modifiers. In this case the images/ and i respectively. The i makes it case insensitive.

Upvotes: 0

genesis
genesis

Reputation: 50976

$new_string = str_replace('images/', 'cms/images/', $string);

Upvotes: 2

Related Questions