Answer the question
In order to leave comments, you need to log in
Processing Text in PHP with Regular Expression
Good day.
Actually I ran into one problem, which I can not solve on my own. It is necessary to process the text entered by users using PHP to pass it to the Sphinx search engine. The search is performed on all available words, not a phrase. To do this, add the + operator before each individual word.
For this purpose I use the following function:
function clean_text_match ($text, $all_words)
{
global $db, $bb_cfg;
$text = ' '. mb_strtolower($text, 'UTF-8') .' ';
if ($all_words)
{
$text = preg_replace('#\s(\b\w)#', ' +$1', $text);
}
$text_match_sql = $db->escape(trim($text));
return $text_match_sql;
}
секс в большом городе
+sex +and +the +city
...
if ($all_words)
{
$text = preg_replace('#\s#', ' +$1', $text);
}
...
+секс +в +большом +городе +
+секс +в +большом +-городе +
+секс +в +большом -городе
Answer the question
In order to leave comments, you need to log in
Try the u modifier:
$text = preg_replace('#\s#u', ' +$1', $text);
www.php.net/manual/en/reference.pcre.pattern.modifiers.php
1. To use Unicode, you need the appropriate modifier .
2. The characters affected by \w depend on the locale. Check the locale, or better yet, replace \w with [a-za-yayo].
public function clean_text_match($text, $all_words) { //global $db, $bb_cfg; $text = ' '. mb_strtolower($text, 'UTF-8') .' '; if ($all_words) { $text = preg_replace('#\s(\b\w)#', ' +$1', $text); } //$text_match_sql = $db->escape(trim($text)); return $text; } public function aaaAction() { echo $this->clean_text_match( 'sex and the city', true ); }
I quote, not mine, but I found it and I'm also interested.
PCRE has special sequences for different classes of Unicode characters, such as "\p{L}" for letters, "\p{N}" for numbers, and so on.
…
First write: $text = preg_replace('#\s(\b\pLN)#', ' +$1', $text);
Well, then there are other methods:
bolknote.ru/2010/09/08/ ~2704#29
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question