Solved

How to make it support Chinese?

Posted on 2004-04-14
1
191 Views
Last Modified: 2006-11-17
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
Comment
Question by:cmkpl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
Fataqui earned 200 total points
ID: 10824533
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question