Solved

How to make it support Chinese?

Posted on 2004-04-14
1
186 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now