Question

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.

$length = 90;
foreach ($words_found as $offset => $word) {
  $word_length = strlen($word);

  $start = $offset - $length;
  $last_start = $start + $length + $word_length;

  $first_part = substr($text, $start, $length);
  $last_part = substr($text, $last_start, $length);

  $sentence = $first_part . '<b>' . $word . '</b>' . $last_part;
}

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.

$word_length = mb_strlen($word, 'UTF-8');
$first_part = mb_substr($text, $start, $length, 'UTF-8');
$last_part = mb_substr($text, $last_start, $length, 'UTF-8');

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?

Was it helpful?

Solution 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.

$first_part_aux = str_split(trim($first_part));

if (!ctype_alpha($first_part_aux[0])) {
  for ($i = 1; $i < count($first_part_aux); $i++) {
    if (ctype_alpha($first_part_aux[$i])) {
      $start = $start + $i;
      $length = $length - $i;

      $first_part = substr($text, $start, $length);

      break;
    }
  }
}

OTHER TIPS

Try using regex with Word Boundaries

$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

if(stripos($string, 'pink ') === 0)
    $pos = 0;
else if(stripos($string, ' pink') !== false)
    $pos = stripos($string, ' pink') + 1;
else
    $pos = stripos($string, ' pink ') + 1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top