wwfc_barmy_army Posted February 3, 2007 Share Posted February 3, 2007 Hello. I need an if-else statement where is looks to see if the field starts with a certain word, how would i go about this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/ Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 $string = substr($text, 0, 5); if ($string == "start") {echo "contains the start string!"} else {echo "doesnt contain the start string!"} I think this is what you want, btw, change 5 to the number of strings of the start word, like "start" has 5 characters. Ted Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176261 Share on other sites More sharing options...
Orio Posted February 3, 2007 Share Posted February 3, 2007 <?php if(strpos($string, $start) === 0) echo "Starts!"; else echo "Doesn't!"; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176262 Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 oh, strpos can change to see the ideal position? never know that. (do orio's) Ted Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176264 Share on other sites More sharing options...
Orio Posted February 3, 2007 Share Posted February 3, 2007 Well, strpos() "Returns the numeric position of the first occurrence of needle in the haystack string" (from the manual). It returns FALSE if it wasn't found, and that's why you need to use the === operator (because 0==FALSE is true, and 0===FALSE is false). Orio. Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176266 Share on other sites More sharing options...
wwfc_barmy_army Posted February 3, 2007 Author Share Posted February 3, 2007 Can you just confirm where the field would go in there, and where the text i am looking for would go in there? is it; $string - where it gets the field $start - the text i want to search for in the field And what does the '=== 0' do? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176269 Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 thanks orio, wwfc_barmy_army, thats right. Ted Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176272 Share on other sites More sharing options...
Orio Posted February 3, 2007 Share Posted February 3, 2007 $string- The string coming from that field of yours. $start- The string you are using to check if it's the start of $string The " === 0" part- Checks if $start was found in the beginning of $string (using the strpos() function). Orio. Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176273 Share on other sites More sharing options...
wwfc_barmy_army Posted February 3, 2007 Author Share Posted February 3, 2007 1 last question, how would i be able to say if it starts with this or this? What i have so far is: <?php if(strpos($qry[link], "http://") === 0) echo "Starts!"; else echo "Doesn't!"; ?> But if i try putting the or operator (||) after it always says 'doesnt'. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176278 Share on other sites More sharing options...
Orio Posted February 4, 2007 Share Posted February 4, 2007 <?php if(strpos($qry[link], "http://") === 0 || strpos($qry[link], "ftp://") === 0) echo "Starts!"; else echo "Doesn't!"; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-176809 Share on other sites More sharing options...
wwfc_barmy_army Posted February 4, 2007 Author Share Posted February 4, 2007 Thanks for all your help Orio! Quote Link to comment https://forums.phpfreaks.com/topic/36941-solved-if-begins-with/#findComment-177101 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.