Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mark up string values if they contain certain words 1

Status
Not open for further replies.

theotrain

Programmer
Mar 5, 2003
150
MX
im making a page to monitor content typed into a kids website. the page just spits out all the new text that has been entered but not yet checked - the monitor can then flag inappropriate content.

i would like to highlight curse words to make it easier for the monitor to scan the text. im starting with a database table of naughty words (is that the best way to store the words?) and what i want is to check a string against all the naughty words.

example string:
Code:
"you are a mother-sucking poophole"
results in:
Code:
"you are a <strong>mother-sucking</strong><strong>poophole</strong>"

so im wondering how i can check the string against EVERY word in the table, and i will need the position of each word found to insert the tags. this hurts my tiny mind, but someone cleverer than i might see this differently.

if i have to implement a solution that just highlights the whole block of text that contains a curse, that would be OK but not ideal.

any ideas appreciated!
 
Say your text is contained in $mytext, and you've retrieved the words from your DB:

Code:
$mytext="you are a mother-sucking poophole";

$res=mysql_query("SELECT wordfield FROM word_table");
...
while($word=mysql_fetch_row($res);

$mytext=str_replace($word[0]," <strong>$word[0]</strong> ",$mytext);

echo $mytext;

This of course gets exact matches. If you want more leeway in matches, you may want to use Regular Expression match instead. Though it may get very slow if there are many words to match.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
thanks!

i had the crazy idea there might be a fancy way to do it where you didnt basically have to loop through each word in the list and check it against every string, since there is an array of strings as well.

but i couldnt come up with anything, so i've done basically what you've suggested with an outer loop to go through each item in the array of strings.

str_replace() is lovely, i thought the actual replacement with markup would be harder than it was.

anyway it works! thank you.
 
Hi

Two things :
[ul]
[li]Better use [tt]str_ireplace()[/tt], the case-insensitive version of [tt]str_replace()[/tt].[/li]
[li]If your database has suitable text handling functions/operators, think about doing the job there. Could be faster than transferring the whole list to the PHP script.[/li]
[/ul]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top