Sammy Miri Sammy Miri - 3 months ago 12
PHP Question

preg_replace entire string if currently contains a string

i would like to replace the entire URL to completely new one if the URL currently contains an unwanted string.

this is what i tried.

if (preg_match('/(')){
$url = preg_replace('/(',(''),$url);

any help is appreciated.


It doesn't seem necessary to do a preg_replace if you want to replace the entire string. So, try this:

$url = '';
$pattern = '/(';
$newUrl = '';

if (preg_match($pattern, $url)){
    $url = $newUrl;