Stargate22 Posted September 17, 2015 Share Posted September 17, 2015 Hello guys $myFile = "smt.txt"; $fh = fopen($myFile, 'r'); $all = fread($fh, filesize($myFile)); $cities = explode ("," , $all); foreach ($cities as $city) { if(0 === strpos($city,'E')){ print_r($city); } } I want to print only the strings from the array $cities that start with E , my code does nothing, not an error, nothing.. if i remove the if and let the print it works.. but I need just thous that start with E. Do you guys have any tips? PS: in my array there are a lot of cities that start with E... Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/ Share on other sites More sharing options...
requinix Posted September 17, 2015 Share Posted September 17, 2015 What does it show if you remove the if? Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521054 Share on other sites More sharing options...
Stargate22 Posted September 17, 2015 Author Share Posted September 17, 2015 @requinix if i remove the if and let the print_r it is printing the array like: New York Los Angeles Edinburgh and so on... Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521055 Share on other sites More sharing options...
requinix Posted September 18, 2015 Share Posted September 18, 2015 Right. I was thinking more of the exact output. And while I'm at it, can you use var_dump() instead? Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521059 Share on other sites More sharing options...
hansford Posted September 18, 2015 Share Posted September 18, 2015 foreach ($cities as $city) { if($city[0] == 'E') { print_r($city); } } Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521060 Share on other sites More sharing options...
Psycho Posted September 18, 2015 Share Posted September 18, 2015 Just show the output of this: var_dump($cities); We want to verify that there is a value that starts with 'E' (not 'e') and that the value doesn't start with a space or some other non-printable character. Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521068 Share on other sites More sharing options...
Stargate22 Posted September 18, 2015 Author Share Posted September 18, 2015 Just show the output of this: var_dump($cities); We want to verify that there is a value that starts with 'E' (not 'e') and that the value doesn't start with a space or some other non-printable character. Hello, It prints the array cities like: array (size=268) 0 => string 'Aboed' (length=5) 1 => string ' Ajold' (length=7) 2 => string ' Afed' (length=6) 3 => string ' ... etc Eugir' (length=7) 82 => string ' Eurtici' (length=9) 83 => string ' .. etc foreach ($new as $city) { $findme = "E"; $pos2 = strpos($city, $findme); #if($pos2 !== false){ #echo "<li>$city</li>"; } } ^-> above code prints all cities that start with E... but why is my initial code not workin`? Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521070 Share on other sites More sharing options...
hansford Posted September 18, 2015 Share Posted September 18, 2015 Then use this if you are not sure of the case: foreach ($cities as $city) { if(strtoupper($city[0]) == 'E') { print_r($city); } } Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521071 Share on other sites More sharing options...
Solution Barand Posted September 18, 2015 Solution Share Posted September 18, 2015 82 => string ' Eurtici' (length=9) Most of the cities in that sample begin with a space (or other non-printing character) and a "\n" In the example I picked out the length is 9 when it should be only 7. You need to trim() the elements in your data Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521072 Share on other sites More sharing options...
Stargate22 Posted September 18, 2015 Author Share Posted September 18, 2015 @Barand Yeap, because I did not trimmed them it did not work, I`m Blind o.O Thank you guys for all the help. @Hansford, thank you for the code! Quote Link to comment https://forums.phpfreaks.com/topic/298202-if-inside-foreach-loop-not-working/#findComment-1521076 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.