Sammy Miri Sammy Miri - 1 year ago 82
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.

Answer Source

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;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download