Jason Paddle Jason Paddle - 9 months ago 38
PHP Question

Remove url and leave only last part of it

I know is something pretty simple for someone who understand well preg_replace but I can't understand how to remove part of url. I have form where user should enter a

. The problem is that some users submitted whole url with
this kind of url




I want when user submit the form to check if he/she inserted whole url and if is to remove everything and leave only

I've tried simple preg_replace like this

$result = preg_replace('https://example.com/page/', '', $str);

But error says

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash

Then I've tried to escape those backslashes with
etc .. but same error occurred.

Any help is appreciated.

Answer Source

Your preg_replace() function should be like this:

$result = preg_replace('$(https|http)://example.com/page/$', '', $str);

You can use $ as a delimiter.