Jump to content

Highlighting matches between string & array


El Robot

Recommended Posts

I'm trying to write an article checker that compares the contents of a textarea with a list of words in an array and highlights any matches.

So far, I have:

[code]
/* Get the form contents */
$article = stripslashes ( $_POST [ 'article' ] );

/* Create array from word list (list from http://www.giwersworld.org/computers/linux/common-words.phtml) */
$wordlist = array_map ( 'rtrim', file ( 'wordlist.txt' ) );

/* Check each word in the list against the article */
foreach ( $wordlist as $word ) {
str_ireplace ( $word, '<strong>'.$word.'</strong>', $article );
}

/* Print results */
echo $article;
[/code]

However, it just echoes back the original article without any new formatting? Can anyone tell me where I'm going wrong?

Thanks.

Probelm is with your string replacement function.When ever you perform a string operation on a variable , it won't modify the original string content.instead of that it will create a new string with the modifications.so you should assign it to a variable.

[code]
str_ireplace ( $word, '<strong>'.$word.'</strong>', $article );

echo $article;

[/code]

replace it with

[code]
<?php

$new_article=str_ireplace ( $word, "<strong>$word</strong>", $article );

echo $new_article;
?>
[/code]

Hope it helps you!

joshi.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.