Home > front end >  i have faced a problem in search for arabic work inside a sentence
i have faced a problem in search for arabic work inside a sentence

Time:08-22

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

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

i always see not founded

any help ?

CodePudding user response:

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";
}

CodePudding user response:

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

CodePudding user response:

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);
}
  • Related