Jump to content

Recommended Posts

I have a form field that will allow a user to post their website for outside linking

 

I want to make sure that the http:// is always present so when i link to it links outside my site.  However I want to make sure there aren't links to ftp:// or https:// and that the http:// is always present any ideas? I'm bad at regex but i'm sure someone knows a way?

Link to comment
https://forums.phpfreaks.com/topic/56747-string-replace-help-with-a-url/
Share on other sites

That code, after trimming any whitespace, just returns the first seven characters of the string, $field.  If those happen to be "http://", then the comparison is true and the if() block is executed.  Since neither "ftp://" or "https://" are "http://", they will not match.  You said you didn't want to match those.  If you decide you do want to match them, you might use a regular expression.

 

if (preg_match('|^(?:https?|ftp)://|i',$field) { /* ...etc */

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.