Jump to content

Noob Regex queston


muffinman

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/146318-noob-regex-queston/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.