Jump to content

Function Looping - How do I loop this?


dreamkiller23

Recommended Posts

<?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

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,

<?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?

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,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.