0

I have an array of locations slugs and a sentence that might have one of the locations. So I want to get the location in the sentence from the locations array

    $areas = 'garki-i,garki-ii,yaba,wuse-i,asokoro,maitama,jabi,jahi,dutse,gwarinpa,central-business-district,kubwa,lugbe,kaura,gudu,banana-island,new-karu,old-karu,kugbo,eko-atlantic,nyanya,mararaba,madalla,kuje,wuse-ii,utako,oulfa,kimunyu,ibara,cfc,joska,kabati,juja';
    $a_arr = explode(',', $areas);
    
    $tweet = "I live in Eko Atlantic and Yaba and I also work at Banana Island";
    $t_arr = explode(" ", strtolower($tweet));
    $location = [];
    
    for ($i = 0; $i < count($t_arr); $i++) {
      for ($j = 0; $j < count($a_arr); $j++) {
        if ($t_arr[$i] == $a_arr[$j]) {
          array_push($location, $a_arr[$j]);
        }
      }
    }
    
    $output = ["eko-atlantic", "yaba", "banana-island"];

I am getting ['yaba'] but I want ["eko-atlantic", "yaba", "banana-island"]

Dominion
  • 152
  • 2
  • 2
  • 13

2 Answers2

1

Here is my solution

<?php 

$areas = 'garki-i,garki-ii,yaba,wuse-i,asokoro,maitama,jabi,jahi,dutse,gwarinpa,central-business-district,kubwa,lugbe,kaura,gudu,banana-island,new-karu,old-karu,kugbo,eko-atlantic,nyanya,mararaba,madalla,kuje,wuse-ii,utako,oulfa,kimunyu,ibara,cfc,joska,kabati,juja';
$a_arr = explode(',', $areas);

$tweet = "I live in Eko Atlantic and Yaba and I also work at Banana Island";
$t_arr = explode(" ", strtolower($tweet));
$location = [];

if ( $t_arr != null ) {
  
  foreach ($a_arr as $key => $value) {

    if ( preg_match ( '/'.str_replace('-', ' ', $value).'/', strtolower($tweet)) ) {
        array_push($location, $value);
    }
  }
}

var_dump( $location );
  • Do you still need to add the $t_arr and its condition, it works fine without the $t_arr – Dominion Jan 05 '23 at 14:29
  • Yeah, I know it is fine but we should use that condition in case of an empty array. – Harsh vaghasiya Jan 05 '23 at 14:57
  • You won't need `strtolower()` if you add `i` to the back of your regex pattern. I recommend using word boundaries in your patterns (`\b`). `$key` is a needless declaration. – mickmackusa Jan 08 '23 at 08:54
  • For the exact same behavior of the current script, `strpos()` could be used instead of `preg_match()`. – mickmackusa Jan 08 '23 at 09:35
  • No, strpos() and preg_match() have different behavior, Here I need to search for multiple words at a time so I used preg_match() instead of strpos(). – Harsh vaghasiya Jan 09 '23 at 11:09
0

You will need to change the inner loop such that it compares the complete string in $t arr[$i] to the entire string in $a arr[$j], rather than just comparing individual characters, in order to alter your code so that it correctly extracts the locations from the tweet. To accomplish this, compare the strings using the strcmp function:

for ($i = 0; $i < count($t_arr); $i++) {
  for ($j = 0; $j < count($a_arr); $j++) {
    if (strcmp($t_arr[$i], $a_arr[$j]) == 0) {
      array_push($location, $a_arr[$j]);
    }
  }
}