cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/56747-string-replace-help-with-a-url/ Share on other sites More sharing options...
Wildbug Posted June 22, 2007 Share Posted June 22, 2007 if (substr(trim($field),0,7) == 'http://') { /* ...etc. */ Quote Link to comment https://forums.phpfreaks.com/topic/56747-string-replace-help-with-a-url/#findComment-280250 Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Author Share Posted June 22, 2007 that 0,7 will that cover if ftp:// also and if i want to include https:// should i make it 8? Quote Link to comment https://forums.phpfreaks.com/topic/56747-string-replace-help-with-a-url/#findComment-280253 Share on other sites More sharing options...
Wildbug Posted June 22, 2007 Share Posted June 22, 2007 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 */ Quote Link to comment https://forums.phpfreaks.com/topic/56747-string-replace-help-with-a-url/#findComment-280283 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.