Help to display values ​​from the table. before comparing them. Suppose there is a sentence:

"EX_WORD was shining today, and CD_WORD will be in the evening"

and I need to bring these words (Sun, Evening) from the table into a sentence.

That is, the proposal after the withdrawal should look like this: "today there was a beautiful SUN , and in the evening it will RAIN ."

The table looks like this:

----------------------------------- id | name | text ------------------------------------- 1 | EX_WORD | солнце 2 | CD_WORD | дождь 

There are about 4000 rows in the table (I showed only 2 rows with an example)

sentences are also 4000 and each sentence has its own values ​​(not only the sun and rain)

  • The simplest solution for php is to get data from the database, break the sentence into an array and then liner serach into a match, you don't want to steam as array_intersect, select the resulting word. If it would be possible to change the table, then hex will be already faster and everything depends on the size of the database. Because you can also add multithreading here. You can also send requests to the database and write LIKE 'WORD', if the offer is small, which solution is more optimal for you, I just don’t want to paint all the options - Dima Kaukin
  • load into associative array, key - name, value - text - TigerTV.ru

1 answer 1

Here is one solution.

 <?php $arrayFromBd = array('солнце', 'дождь'); $str = "сегодня стояло прекрасное СОЛНЦЕ, а вечером будет ДОЖДЬ."; $strLower = mb_strtolower($str); $matchWords = array_intersect(explode(' ', preg_replace('/[.,]/', '', $strLower)), $arrayFromBd); $ind = 0; foreach($matchWords as $val) { $str = substr_replace($str, "<b>".mb_strtoupper($val)."<b>", strpos($strLower, $val)+6*$ind) . substr($str, strpos($strLower, $val)+strlen($val)+6*$ind); $ind++; } echo $str; // сегодня стояло прекрасное <b>СОЛНЦЕ<b>, а вечером будет <b>ДОЖДЬ<b>. 

Or, immediately pull the result sample for the query in the database

 SELECT * FROM MyTable WHERE Column1 LIKE '%сегодня%' OR Column1 LIKE '%стояло%' OR Column1 LIKE '%прекрасное%' ... 

Also request with in

 SELECT * FROM MyTable WHERE name in ('сегодня','стояло','прекрасное') 

And about what I commented

Complex algorithm for a small array ~ 1000

 CPU times: user 4.56 ms, sys: 136 µs, total: 4.7 ms Wall time: 4.09 ms 

A simple algorithm for a small array of ~ 1000

 CPU times: user 263 µs, sys: 63 µs, total: 326 µs Wall time: 333 µs 
  • the table is large, about 4,000 different lines .... I gave an example of one of the lines in the table .... - roharrim2016
  • you can hardly understand more precisely, if in general the number does not reach ~ 10k, complex algorithms are not really needed, acceleration will be noticeable after 50k, well, this is the example of python and that array_intersect tolerates ~ 15k, just the same python I measured the result linear search, was 14 times faster up to 1000, after ~ 50k, it just bent, and the result of the second algorithm was slightly slower than c ~ 1k, well, or a query with line or in, and then test what is better for you - Dima Kaukin