Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

How to make it support Chinese?

Here is the program segment:
$bool_var=preg_match("/\b$search_item\b/i", $contents);


$search_item is the word I need to search in the $contents
It returns TRUE when $contents contains the word we want to search

in this case:
if I search for apple
apple is true, but apples is not true

but Why can't I search for Chinese words?
0
cmkpl
Asked:
cmkpl
1 Solution
 
FataquiCommented:
Hi

If you are using $var(s) inside your preg_match search regex string then you need to make the function understand that your $var(s) are a reference to another value! Maybe you might understand what I mean so I will give you an example....

If I was to do this................ ECHO(s) -> NOT FOUND

<?

$content = 'just some text that contains the word ¥¢¥Ã¥×¥ë¥Ñ¥¤ <- "apple". Can pre_match find it?';

$search = '¥¢¥Ã¥×¥ë¥Ñ¥¤';

if ( preg_match ( '!$search!', $content ) )
{

echo 'FOUND';

}
else
{

echo 'NOT FOUND';

}


?>



The function would echo "NOT FOUND", because it is not looking for the value of $search but the literally word 'search'. Now because you are searching different character set you should 'quote' the value passed to your preg_match function! This way the function will work, example....


If I was to do this................ ECHO(s) -> FOUND

<?

$content = 'just some text that contains the word ¥¢¥Ã¥×¥ë¥Ñ¥¤ <- "apple". Can pre_match find it?';

$search = '¥¢¥Ã¥×¥ë¥Ñ¥¤';

if ( preg_match ( '!' . preg_quote ( $search ) . '!', $content ) )
{

echo 'FOUND';

}
else
{

echo 'NOT FOUND';

}


?>


Fataqui!

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now