El Robot Posted November 15, 2006 Share Posted November 15, 2006 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. Link to comment https://forums.phpfreaks.com/topic/27322-highlighting-matches-between-string-array/ Share on other sites More sharing options...
joshi_v Posted November 15, 2006 Share Posted November 15, 2006 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. Link to comment https://forums.phpfreaks.com/topic/27322-highlighting-matches-between-string-array/#findComment-124903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.