Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.