In PHP, get the full word from a MySQL search result using "LIKE"

what I want: Suppose I searched for "goo" using a query that looks something like this: ...WHERE message LIKE '%goo%' and it returned me a result, for example I love Google to make my searches, but I'm starting to worry about privacy , so it will be displayed as a result because the word Google matches my search criteria.

How can I, based on my search string, save all this Google result for a variable? I need this because I use a regular expression that will highlight the search word and display the content before and after this result, but it only works when the search word exactly matches the word in the result, as well as it is poorly designed, so it won well work with words that are not surrounded by space.

This is regex code

 <?=preg_replace('/^.*?\s(.{0,'.$size.'})(\b'.$_GET['s'].'\b)(.{0,'.$size.'})\s.*?$/', '...$1<strong>$2</strong>$3...',$message);?> 

I want to change this variable $ _GET ['s'] to my variable, which will contain all the word found in my query string.

How do I achieve this?

+4
source share
3 answers

I am reading your discussion on this , and a more robust implementation might be fine. Especially considering diacritics support. Using one regex to fix all your problems may seem tempting, but the more complex it is, the harder it will support or expand it. Quote Jamie Zawinski

Some people, faced with a problem, think: "I know, I will use regular expressions." Now they have two problems.

Since I have problems with iconv on my local machine, I used a simpler implementation instead, feel free to use something more complex or reliable if your situation requires it.

I use a simple regular expression in this solution to get only a set of alphanumeric characters (also known as a "word"), the part in the regular expression that reads \p{L}\p{M} ensures that we also get all multibyte characters .

You can see this code working on IDEone .

 <?php function stripAccents($p_sSubject) { $sSubject = (string) $p_sSubject; $sSubject = str_replace('æ', 'ae', $sSubject); $sSubject = str_replace('Æ', 'AE', $sSubject); $sSubject = strtr( utf8_decode($sSubject) , utf8_decode('àáâãäåçèéêëìíîïñòóôõöøùúûüýÿÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝ') , 'aaaaaaceeeeiiiinoooooouuuuyyAAAAAACEEEEIIIINOOOOOOUUUUY' ); return $sSubject; } function emphasiseWord($p_sSubject, $p_sSearchTerm){ $aSubjects = preg_split('#([^a-z0-9\p{L}\p{M}]+)#iu', $p_sSubject, null, PREG_SPLIT_DELIM_CAPTURE); foreach($aSubjects as $t_iKey => $t_sSubject){ $sSubject = stripAccents($t_sSubject); if(stripos($sSubject, $p_sSearchTerm) !== false || mb_stripos($t_sSubject, $p_sSearchTerm) !== false){ $aSubjects[$t_iKey] = '<strong>' . $t_sSubject . '</strong>'; } } $sSubject = implode('', $aSubjects); return $sSubject; } /////////////////////////////// Test \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ $aTest = array( 'goo' => 'I love Google to make my searches, but I`m starting to worry about privacy.' , 'peo' => 'people, People, PEOPLE, peOple, people!, people., people?, "people, people" péo' , 'péo' => 'people, People, PEOPLE, peOple, people!, people., people?, "people, people" péo' , 'gen' => '"gente", "inteligente", "VAGENS", and "Gente" ...vocês da física que passam o dia protegendo...' , 'voce' => '...vocês da física que passam o dia protegendo...' , 'o' => 'Characters like æ,ø,å,Æ,Ø and Å are used in Denmark, Sweden and Norway' , 'ø' => 'Characters like æ,ø,å,Æ,Ø and Å are used in Denmark, Sweden and Norway' , 'ae' => 'Characters like æ,ø,å,Æ,Ø and Å are used in Denmark, Sweden and Norway' , 'Æ' => 'Characters like æ,ø,å,Æ,Ø and Å are used in Denmark, Sweden and Norway' ); $sContent = '<dl>'; foreach($aTest as $t_sSearchTerm => $t_sSubject){ $sContent .= '<dt>' . $t_sSearchTerm . '</dt><dd>' . emphasiseWord($t_sSubject, $t_sSearchTerm) .'</dd>'; } $sContent .= '</dl>'; echo $sContent; ?> 
+2
source

I bet it will be easier to change your regular expression to check for any word containing this term, and that:

 <?=preg_replace('/^.*?(.{0,'.$size.'})(\b\S*'.$_GET['s'].'\S*\b)(.{0,'.$size.'}).*?$/i', '...$1<strong>$2</strong>$3...',$message);?> 
+4
source

I don’t understand the importance of matching everything else in the search bar, is this enough?

 <?=preg_replace('/\b\S*'.$GET['s'].'\S*\b/i', '<strong>$0</strong>', $message);?> 

As far as I can tell, you only put the matching word in the html tag but do nothing with the rest of the line?

The above regex is great for cases where you are only matching whole words, capturing multiple matches within a string (must be more than one), and also works great with case insensitivity.

0
source

All Articles