system
November 7, 2008, 8:59am
1
Hi,
Normally we use preg_match() to search the given string, and it returns 1 or 0.
while using it to search for array values,
for eg,
$arr = array();
$tt = preg_match(“[0-9]”, $arr);
it shows warning as,
Warning: preg_match() expects parameter 2 to be string, array given in e:\ est.php on line 27.
How to check for array values???
oddz
November 7, 2008, 9:11am
2
What do you mean by array values the array in your example is empty?
If you head over to the manual you should see that preg_match accepts only strings as the first two arguments.
ro88o
November 7, 2008, 9:55am
3
If I understand correctly what you’re trying to do, you could loop through the array, check each string value in the array with preg_match and break out of it if you find a match.
e.g.
$arr = array();
//Add data into the array here
for ($i = 0; $i < count($arr); $i++) {
$tt = preg_match("[0-9]", $arr[$i]);
if ($tt == 1) {
break;
}
}
tdolsen
November 7, 2008, 10:25am
4
You could take a look at preg_grep .
ro88o:
If I understand correctly what you’re trying to do, you could loop through the array, check each string value in the array with preg_match and break out of it if you find a match.
e.g.
for ($i = 0; $i < $arr.length; $i++)
{
$tt = preg_match("[0-9]", $arr);
if ($tt == 1)
break;
}
You, of course, meant:
for ($i = 0; $i < count($arr); $i++) {
$tt = preg_match("[0-9]", $arr[$i]);
if ($tt == 1) {
break;
}
}
ro88o
November 7, 2008, 12:55pm
6
Ha, yep I did Been spending too much time developing JS recently and in too much of a rush to check my code!