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! 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 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 = ''; } ?> 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! 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
Archived
This topic is now archived and is closed to further replies.