www.webdeveloper.com
Results 1 to 2 of 2

Thread: Searching a string for substring that is in an array.

  1. #1
    Join Date
    May 2007
    Posts
    184

    Searching a string for substring that is in an array.

    Basically I have an array of small strings. Then I have a larger string in which I want to see if any of those in the array of smaller strings match anything in the larger string. Is there a function in php in which I can pass the array of smaller strings and the larger string and get a bool value or something like that? Or am I going to have to loop through each array index and do a preg_match on each to get a result? It doesn't matter which smaller string matches? Baiscally I want to see if any of the smaller strings match or if none match the larger one.

    Any advice is appreciated. Thanks in advance.

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    preg_match does not support array searching, so you will have to loop, something like this should be sufficient:
    PHP Code:
    function preg_Amatch($pattern$string){
      foreach(
    $pattern as $tmp){
        if(
    preg_match($tmp$string)){
          return 
    true;
        };
      };
      return 
    false;
    }; 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles