Mhammed Talhaouy
Mhammed Talhaouy

Reputation: 1259

How to search for an Arabic word inside an array?

Hi everyone i have faced a problem during my working to return a response for a api the main problem is

$name = "تش";
$restaurants =   [
  "تشك تشيكن",
  "بيتزا ساخنة",
  "كينتاكي",
  "تشيكن سبوت"
];

foreach ($restaurants as $restaurant) {
  if (strpos($restaurant, $name)) {
    echo "founded";
  }
}

any help ?

Upvotes: 2

Views: 87

Answers (4)

Mhammed Talhaouy
Mhammed Talhaouy

Reputation: 1259

I have found the answer finally

   $name = "تش";
    $restaurants =   [
      "تشك تشيكن",
      "بيتزا ساخنة",
      "كينتاكي",
      "تشيكن سبوت"
    ];
    
    foreach ($restaurants as $restaurant) {
      if (strpos($restaurant, $name) !== false) {
        echo "founded";
      }
    }

Upvotes: 0

Rain
Rain

Reputation: 3936

The first argument in strpos should be the haystack, which is the string you want to search in, and the second argument is the needle, which is the string you want to search for

    <?php
    
    $item = "أهلا وسهلا بكم";
    $search = "وسهلا";

     // look if $search is in $item
    if (strpos($item, $search)){
       echo "founded";
    }else {
    echo "not founded";
    }

https://3v4l.org/C07o8

Upvotes: 1

Mohamed Ghulam
Mohamed Ghulam

Reputation: 537

Your first argument should be $item and then the keyword you are searching for which is $search

So your code should look like:

$item = "أهلا وسهلا بكم";
$search = "وسهلا";
if (strpos($item, $search)){
    echo "founded";
}else {
    echo "not founded";
}

Upvotes: 1

Malik12tree
Malik12tree

Reputation: 44

based on this duplicate, Arabic characters are encoded using multibyte characters, so you need to use grapheme_strpos()

if (function_exists('grapheme_strpos')) {
    $pos = grapheme_strpos($tweet, $keyword);
} elseif (function_exists('mb_strpos')) {
    $pos = mb_strpos($tweet, $keyword);
} else {
    $pos = strpos($tweet, $keyword);
}

Upvotes: 0

Related Questions