Warning: preg_match() expects parameter 2 to be string, array given in e:\\test.php on

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

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.

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;
    }
} 

You could take a look at preg_grep.

You, of course, meant:


for ($i = 0; $i < count($arr); $i++) {
	$tt = preg_match("[0-9]", $arr[$i]);
	if ($tt == 1) {
          break;
	}
}

:wink:

Ha, yep I did :slight_smile: Been spending too much time developing JS recently and in too much of a rush to check my code!