phparraysmetaphone

How to search for a part of a string in an array?


I want to search whether the complete string or a part of the string is a part of the array. How can this be achieved in PHP?

Also, how can I use metaphone in it as well?

Example:

array1={'India','USA','China'};
array2={'India is in east','United States of America is USA','Made in China'}

If I search for array1 in array2, then:

'India' should match 'India is in east' and similarly for USA & China.


Solution

  • $a1 = array('India','USA','China');
    $a2 = array('India is in east','United States of America is USA','Made in China');
    
    
    foreach ( $a2 as $a )
    {
      foreach( $a1 as $b  )
      {
        if ( strpos( $a, $b ) > -1 )
        {
          echo $a . " contains " . $b . "\n";
        }
      }
    }