2

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 ?

Mhammed Talhaouy
  • 1,129
  • 10
  • 14

4 Answers4

1

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";
}
Mohamed Ghulam
  • 457
  • 3
  • 7
1

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

Rain
  • 3,416
  • 3
  • 24
  • 40
0

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);
}
Malik12tree
  • 44
  • 1
  • 5
  • 1
    That's not the issue. The order of the arguments are incorrect, as others mentioned. The position is irrelevant if the intention is to find a sequence of characters. – Pedro Amaral Couto Aug 21 '22 at 22:10
0

I have found the answer finally

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