Jump to content

String Replace help with a URL


cooldude832

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 */

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.