|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
problems with str_replace
Hi,
am very new to php and am trying to strip swear words listed in a database from a variable and replace them with blank space. Can anyone help? php code: ------------------------------------------------------------------------------- <?php mysql_select_db($database_icarus, $icarus); $query_rsDot = "SELECT extension FROM bad"; $rsDot = mysql_query($query_rsDot, $icarus) or die(mysql_error()); $row_rsDot = mysql_fetch_assoc($rsDot); $totalRows_rsDot = mysql_num_rows($rsDot); $text = "blah blah blah xxxx blah2 blah2"; $text2 = str_replace (array("$rsDot"), " ","$text"); ?> -------------------------------------------------------------------------------- I was try to put the database results ($rsDot) into an array, replace all occurences in $test with blank space, and then display the results as $text2 I am not getting any errors but it is not taking the words out. |
|
#2
|
|||
|
|||
|
It's close but you'd be better of with this;
PHP Code:
Note that mysql_query() returns a resource not an array. |
|
#3
|
|||
|
|||
|
that would almost work except for one thing
$text = str_replace ($row_rsDot, '*bleep*',$text); should be $text = str_replace ($row_rsDot[0], '*bleep*',$text); dont forget $row_rsDot = mysql_fetch_assoc($rsDot)) makes $row_rsDot an array |
|
#4
|
|||
|
|||
|
Good point. Attention wandered there for a moment.
|
|
#5
|
|||
|
|||
|
many thanks Harry, many thanks Ben. Actually Harry your code worked first time and when I made Ben's changes it wouldn't work! Thanks again,
cheeri Anders |
|
#6
|
|||
|
|||
|
thats weird, as you cant replace an "array" only an array value!
|
|
#7
|
|||
|
|||
|
Why wont file() work with str_replace()??
I have a similar problem getting str_replace() to work.
Here's my following code: <?php $words = file("badwords.txt"); trim($words); $search = "The bad kitty said doh and hello."; $num = count($words); $i = 0; while($i < $num) { $search = str_replace($words[$i], '*bleep*' , $search); $i++; } echo "<br>"; echo $search; ?> I think it has something to do with the file() bc the following code works: <?php $words = array( "bad", "kitty", "hello"); $search = "The bad kitty said doh and hello."; $num = count($words); $i = 0; while($i < $num) { $search = str_replace($words[i], '*bleep*' , $search); i++; } echo "<br>"; echo $search; ?> |
|
#8
|
|||
|
|||
|
ben, shouldn't it be
PHP Code:
it's an associative array, maybe that's why he couldn't get it to work using $text = str_replace ($row_rsDot[0], '*bleep*',$text); |
|
#9
|
|||
|
|||
|
Will your bad word filter replace case insensative? str_replace() is case sensative, so, it might get BAD_WORD but will not get BaD_WoRd, or other combinations of bad_word. Below is a more robust solution to your dillema ->
PHP Code:
So use that case insensative function. Now, build your array of words to look for and iterate over a copy of the array() like so -> PHP Code:
Where $post holds the data you are searching through for the bad words.... |
|
#10
|
|||
|
|||
|
or juse use ereg_replace
|
|
#11
|
|||
|
|||
|
Or preg_replace().....
![]() |
|
#12
|
|||
|
|||
|
or eregi_replace()...
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > problems with str_replace |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|