dreamwest Posted January 28, 2009 Share Posted January 28, 2009 If i have 2 strings: 1234 and HD1234 How can i tell php to only use "HD1234" Quote Link to comment https://forums.phpfreaks.com/topic/142877-if-string-contains/ Share on other sites More sharing options...
DarkWater Posted January 28, 2009 Share Posted January 28, 2009 What? Quote Link to comment https://forums.phpfreaks.com/topic/142877-if-string-contains/#findComment-749053 Share on other sites More sharing options...
corbin Posted January 28, 2009 Share Posted January 28, 2009 http://php.net/strpos maybe? Quote Link to comment https://forums.phpfreaks.com/topic/142877-if-string-contains/#findComment-749058 Share on other sites More sharing options...
Prismatic Posted January 28, 2009 Share Posted January 28, 2009 <?php $strings = array("the quick brown fox 1234", "the quick brown fox HD1234"); for($i = 0; $i < count($strings); $i++) { if(strpos($strings[$i], "HD1234")) { echo $strings[$i]; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142877-if-string-contains/#findComment-749060 Share on other sites More sharing options...
DarkWater Posted January 28, 2009 Share Posted January 28, 2009 @Prismatic: I can't really think of any valid reason to ever use a for loop like this in PHP for arrays. for($i = 0; $i < count($strings); $i++) { foreach would do it better: foreach($strings as $key=>$string) { Quote Link to comment https://forums.phpfreaks.com/topic/142877-if-string-contains/#findComment-749063 Share on other sites More sharing options...
.josh Posted January 29, 2009 Share Posted January 29, 2009 If i have 2 strings: 1234 and HD1234 How can i tell php to only use "HD1234" be more specific. Are you meaning to say that sometimes the string might be 1234 and sometimes it might be HD1234 and you only want to do something if it's HD1234? Quote Link to comment https://forums.phpfreaks.com/topic/142877-if-string-contains/#findComment-749073 Share on other sites More sharing options...
dreamwest Posted January 29, 2009 Author Share Posted January 29, 2009 Are you meaning to say that sometimes the string might be 1234 and sometimes it might be HD1234 and you only want to do something if it's HD1234? Yes. Only if it has "HD" in the string. Is there a contains function like this?? if str_contains("HD"){ Then do it }else{ Dont do it } Quote Link to comment https://forums.phpfreaks.com/topic/142877-if-string-contains/#findComment-749115 Share on other sites More sharing options...
.josh Posted January 29, 2009 Share Posted January 29, 2009 If that is the only thing in your string (not some number in a larger string), then you can do this: $string = 'HD1234'; if (preg_match('~^HD[0-9]+$~',$string)) { // matches, do something } Quote Link to comment https://forums.phpfreaks.com/topic/142877-if-string-contains/#findComment-749118 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.