Jeffro Posted April 25, 2011 Share Posted April 25, 2011 How would I write this in php?: If $url does not begin with 'http://' $url = '' I want to assign nothing to the variable if it doesn't begin with http:// Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234622-how-to-empty-variable-if-it-doesnt-begin-with-http/ Share on other sites More sharing options...
kenrbnsn Posted April 25, 2011 Share Posted April 25, 2011 I would use substr <?php if (substr($url,0,7) != 'http://') { $url = ''; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/234622-how-to-empty-variable-if-it-doesnt-begin-with-http/#findComment-1205725 Share on other sites More sharing options...
Fadion Posted April 25, 2011 Share Posted April 25, 2011 Personally I would use strpos() <?php if (strpos($url, 'http://') === FALSE) { $url = ''; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234622-how-to-empty-variable-if-it-doesnt-begin-with-http/#findComment-1205726 Share on other sites More sharing options...
Jeffro Posted April 25, 2011 Author Share Posted April 25, 2011 I would use substr <?php if (substr($url,0,7) != 'http://') { $url = ''; } ?> Ken Worked like a charm. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/234622-how-to-empty-variable-if-it-doesnt-begin-with-http/#findComment-1205728 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.