I have a simple PHP code to get a sentences of a text and bold an specific word.
First of all I get an array with the words that I want and their position in the text.
$all_words = str_word_count($text, 2, 'åæéø');
// $words is an array with the words that I want find.
$words_found = array();
foreach ($all_words as $pos => $word_found) {
foreach ($words as $word) {
if ($word == strtolower($word_found)) {
$words_found[$pos] = $word_found;
break;
}
}
}
Then, for every word in $words_found I get a portion of the text with the word in the middle.
It works fine excepts that the text is a UTF-8 text with danish characteres (åæéø). So when $first_part or $last_part starts by an unicode character the susbtr string is empty.
I know mb_substr function, so I replace my code with it.
But with this function (mb_substr) the position of the word ($offset) is wrong, the new substrings ($sentence) doesn't match as it should be.
Does it exist something like mb_str_word_count? How can I get a the correct position of the words?
해결책 2
I try the solution by @Mario Johnathan but it didn't work properly for me.
Finally I get a solution by my own: I use the non multi-byte functions like substr and the position given by str_word_count, and the solution is changing the first substring if the first character is a danish character.
$string = 'That this notpink a or pink blue red dark.';
$regex = '/\bpink\b/';
preg_match($regex, $string, $match, PREG_OFFSET_CAPTURE);
$pos = $match[0][1];
echo $pos;
Edit :
If you don't like regex, you can match word with stripos by using space