Is there a function to check if a string contains an integer?
I know strpos() can check for a single #. But i need it to check for numbers 0-9 and don't feel like writing strpos() for each #.
Is there a function to check if a string contains an integer?
I know strpos() can check for a single #. But i need it to check for numbers 0-9 and don't feel like writing strpos() for each #.
Check if the string is numeric or check for the existance of an integer (or more than one) in a given string?
Ah, i think i know what you are after so I'll just answer the question.
If you want to look for number(s) in a string, this should work:
if (ereg("[0-9]", $string)) {//...Do your thing}
I was pretty much generating a random string that could be composed of letters and #s. But of course some instances output only letters, so I wanted to check if the outputted string contained a #, and if it didn't generate a random string again etc.
function forgot_pass()
{
global $chars, $i, $pass, $num, $tmp;
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7)
{
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
if (ereg('[0-9]', $pass))
{
return $pass;
}
else
{
forgot_pass();
}
}
Here is the function I was working on. It seemed to be working perfectly fine. Tried it about 8 times and each string had a # in it. On the 9th try there was no result. So I kept trying again, and every 4th or 5th try I'd do, I would get no value once again. Can't seem to figure out why that is though.
The blank one happens when the check fails and the function calls itself again.
BTW preg_match('/[0-9]/', $pass) is much faster than ereg.
My theory is that the function has nothing to return once it falls into a deeper recursive level. This theory seems true based on my rewrite of your code that works:
function forgot_pass()
{
global $chars, $i, $pass, $num, $tmp;
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7)
{
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass .= $tmp;
$i++;
}
if (preg_match('/[0-9]/', $pass))
{
return($pass);
}
else
{
return forgot_pass();
}
}
Thank you for your help. Everything works perfectly fine now. All that's left is for me to fully understand the changes you made.
From: $pass = $pass . $tmp;
To: $pass .= $tmp;
Is pretty much the only thing I don't get.
Oh sorry,
That was an anal change and not crucial to the outcome. Both result in the same thing. In fact, the only crucial step was using a return on the recursive call in order to "bubble" out the final result.
When I began Web development, it was using ASP and VBScript. I got used to writing stuff like:
$pass = $pass . $tmp;
which is fine. However, you can write less code if you use the "append" style:
$pass .= $tmp;
just means whatever $pass is, just append $tmp. There are similiar operators for numbers:
$subtotal += $itemPrice;
or take the current subtotal and add the current item price to it.
Cheers!