simonp Posted June 2, 2010 Share Posted June 2, 2010 Hi, I'm looking to use regex to ensure a field begins with http:// Can anyone tell me how I can do that? Thanks Simon Quote Link to comment https://forums.phpfreaks.com/topic/203626-regex-help/ Share on other sites More sharing options...
Adam Posted June 2, 2010 Share Posted June 2, 2010 You don't need regexp for that, you can just use substr: if (substr($str, 0, 7) == 'http://') { Quote Link to comment https://forums.phpfreaks.com/topic/203626-regex-help/#findComment-1066597 Share on other sites More sharing options...
salathe Posted June 2, 2010 Share Posted June 2, 2010 You don't need substr for that, you can use strpos: if (strpos($str, 'http://') === 0) { Quote Link to comment https://forums.phpfreaks.com/topic/203626-regex-help/#findComment-1066598 Share on other sites More sharing options...
Adam Posted June 2, 2010 Share Posted June 2, 2010 Touché! Quote Link to comment https://forums.phpfreaks.com/topic/203626-regex-help/#findComment-1066600 Share on other sites More sharing options...
simonp Posted June 2, 2010 Author Share Posted June 2, 2010 ok you two - that's scary! Unforuntately, it's being checked by a third party piece of software that I need to put in a regexp Any ideas? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/203626-regex-help/#findComment-1066601 Share on other sites More sharing options...
Adam Posted June 2, 2010 Share Posted June 2, 2010 Yar: if (preg_match('#^http://#', $str)) { Using "#" as the delimiter or it just looks ridiculous. Quote Link to comment https://forums.phpfreaks.com/topic/203626-regex-help/#findComment-1066604 Share on other sites More sharing options...
simonp Posted June 2, 2010 Author Share Posted June 2, 2010 Thanks MrAdam - simple! Quote Link to comment https://forums.phpfreaks.com/topic/203626-regex-help/#findComment-1066609 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.