muffinman Posted February 22, 2009 Share Posted February 22, 2009 I have a quick question that I have not been able to find an answer to with the search function. I am trying to strip out the domain portion of a url or partial url from user input and replace it with something else. I must admit I an somewhat new to regex and have been using some patterns that I was able to locate; they both work when only something like: www.example.com or example.com or http://www.example.com. They will strip out the "example.com" portion and leave the remainder but they fail when there is more in the string. Say I try "this is www.example.com" it does not replace the example.com portion and leaves the entire input. I have tried the following patterns: $pattern = '/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$/'; and $pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i'; with preg_replace: $string=preg_replace($pattern, '[LINK REMOVED]', $input); with $input being the user input from a form. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/146318-noob-regex-queston/ Share on other sites More sharing options...
sasa Posted February 22, 2009 Share Posted February 22, 2009 try <?php $test = 'http://www.phpfreaks.com/forums/index.php/topic,239826.0.html'; $a = parse_url($test); //print_r($a); echo $test = str_replace($a['host'], '[LINK REMOVED]', $test); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146318-noob-regex-queston/#findComment-768288 Share on other sites More sharing options...
muffinman Posted February 22, 2009 Author Share Posted February 22, 2009 But will that work with other user input as well? Like if the url were in the middle of a sentence? Like: "Please visit the following page: http://www.phpfreaks.com. "? Quote Link to comment https://forums.phpfreaks.com/topic/146318-noob-regex-queston/#findComment-768314 Share on other sites More sharing options...
muffinman Posted February 22, 2009 Author Share Posted February 22, 2009 I was able to work out was I was having trouble with here: http://www.gskinner.com/RegExr/. Quote Link to comment https://forums.phpfreaks.com/topic/146318-noob-regex-queston/#findComment-768570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.