Solved

How to make it support Chinese?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

18 Experts available now in Live!

Get 1:1 Help Now