SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member sankrish's Avatar
    Join Date
    May 2008
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:\test.php on line 27.
    How to check for array values?????

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,187
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    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.

  3. #3
    SitePoint Enthusiast ro88o's Avatar
    Join Date
    Jun 2006
    Location
    Manchester, UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sankrish View Post
    Normally we use preg_match() to search the given string, and it returns 1 or 0.
    How to check for array values?????
    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.
    PHP Code:
    $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;
        }

    Last edited by ro88o; Nov 7, 2008 at 07:01. Reason: Corrected code
    www.nubornis.com
    Web Development from Nuborn Internet Services

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Location
    Oslo, Norway
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could take a look at preg_grep.

  5. #5
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ro88o View Post
    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.
    PHP Code:
    for ($i 0$i $arr.length$i++)
    {
         
    $tt preg_match("[0-9]"$arr);
         if (
    $tt == 1)
              break;

    You, of course, meant:

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


  6. #6
    SitePoint Enthusiast ro88o's Avatar
    Join Date
    Jun 2006
    Location
    Manchester, UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha, yep I did Been spending too much time developing JS recently and in too much of a rush to check my code!
    www.nubornis.com
    Web Development from Nuborn Internet Services


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •