Mike
Mike

Reputation: 11

PHP Preg_replace() function

How to use the preg_replace function to replace

/c83403.403/

Example: https://startimage.ca/c83403.403/ahmedmynewpix.jpg

another example: https://startimage.ca/c2.3403.403/ahmedmynewpix2.jpg

It always start with /c..../ I want to replace that with ""

I am trying the following but not working $str = '/c..../'; $str = preg_replace('/+[0-9]', '', $str);

Upvotes: 0

Views: 359

Answers (1)

raina77ow
raina77ow

Reputation: 106385

Do you mean something like this?

$str = 'https://startimage.ca/c83403.403/ahmedmynewpix.jpg';
$str = preg_replace('|/c[0-9.]+|', '', $str);
echo $str; # https://startimage.ca/ahmedmynewpix.jpg'

... or

$str = preg_replace('|/c[0-9.]+|', '/c', $str);
echo $str; # https://startimage.ca/c/ahmedmynewpix.jpg'

The point is that you replace anything starting from /c and containing either digits or dot symbol (.) - with either empty space or /c string, depending on what you need. )

Upvotes: 2

Related Questions