dreamkiller23 Posted March 31, 2008 Share Posted March 31, 2008 <?php Function pickup_letter_digit($check){ $pattern_string='^[A-Za-z][0-9]$'; $result=substr($check, 2,2); $num=ereg($pattern_string,$result); if ($num == true){ return $result; }else{ return false; } } $number=zzz4k3; if (pickup_letter_digit($number) == false){ print "Sorry error";} else{ $returned=pickup_letter_digit($number); print_r($returned); } ?> So basically what I want my program to do is like the $number string has in it letters and number, I want it to pickup and return all the lowercase letters and the number that follows. It works as it it, but the output is: z4. I want it to print out the k3 also. How would I write a loop to do this. I tried using a for loop and the count function but that doesn't seem to get me very far. Please help! Link to comment https://forums.phpfreaks.com/topic/98819-function-looping-how-do-i-loop-this/ Share on other sites More sharing options...
uniflare Posted March 31, 2008 Share Posted March 31, 2008 try using preg_match_all to split the string where needed into an array, eg: <?php $str = "zzz4k3ll5"; $res = preg_match_all("/[a-z]+[0-9]{1}/",$str,$matches); print_r($matches); exit("\n\n".$res); ?> would echo: Array ( [0] => Array ( [0] => zzz4 [1] => k3 [2] => ll5 ) ) 3 hope this helps, Link to comment https://forums.phpfreaks.com/topic/98819-function-looping-how-do-i-loop-this/#findComment-505652 Share on other sites More sharing options...
dreamkiller23 Posted March 31, 2008 Author Share Posted March 31, 2008 Hmm, while this does help me alot ALOT, I'd of like the output to be: z4 k5 Or in the way you have it, just print the zzz4k3ll5 z4 k3 l5 Link to comment https://forums.phpfreaks.com/topic/98819-function-looping-how-do-i-loop-this/#findComment-505654 Share on other sites More sharing options...
dreamkiller23 Posted March 31, 2008 Author Share Posted March 31, 2008 <?php function pickup_letter_digit($check){ $count = count($check); $pattern_string='^[A-Za-z][0-9]$'; for($i = 0; $i <= $count; $i++){ $result=substr($check, $i,2); $num=ereg($pattern_string,$result); if ($num == true){ return $result; } } } $number=Abc25k3; if (pickup_letter_digit($number) == true){ $returned=pickup_letter_digit($number); print_r($returned); } ?> Still nothing. Any ideas? Link to comment https://forums.phpfreaks.com/topic/98819-function-looping-how-do-i-loop-this/#findComment-505663 Share on other sites More sharing options...
uniflare Posted March 31, 2008 Share Posted March 31, 2008 Sorry i was busy answering another topic, am i right in thinking you only want 1 letter and 1 digit? Just tweak the pattern like so: $res = preg_match_all("/[a-z]{1}[0-9]{1}/",$str,$matches); ------ $res = number of matches, $matches = the resulting array. hope this helps, Link to comment https://forums.phpfreaks.com/topic/98819-function-looping-how-do-i-loop-this/#findComment-505679 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.