General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old October 15th, 2002, 04:24 AM
anderpants anderpants is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: England
Posts: 10 anderpants User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 32 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old October 15th, 2002, 04:24 PM
HarryF HarryF is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 17 HarryF User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It's close but you'd be better of with this;

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());
$totalRows_rsDot mysql_num_rows($rsDot);

$text "blah blah blah xxxx blah2 blah2"
while (
$row_rsDot mysql_fetch_assoc($rsDot)) {
     
$text str_replace ($row_rsDot'*bleep*',$text);
}
?>


Note that mysql_query() returns a resource not an array.

Reply With Quote
  #3  
Old October 15th, 2002, 07:27 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #4  
Old October 16th, 2002, 03:05 AM
HarryF HarryF is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 17 HarryF User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Good point. Attention wandered there for a moment.

Reply With Quote
  #5  
Old October 16th, 2002, 08:08 AM
anderpants anderpants is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: England
Posts: 10 anderpants User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 32 sec
Reputation Power: 0
Thumbs up

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

Reply With Quote
  #6  
Old October 16th, 2002, 07:57 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
thats weird, as you cant replace an "array" only an array value!

Reply With Quote
  #7  
Old October 20th, 2002, 06:24 PM
supfade_ supfade_ is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 1 supfade_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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;


?>

Reply With Quote
  #8  
Old October 21st, 2002, 02:38 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
ben, shouldn't it be
PHP Code:
 $text str_replace ($row_rsDot['extension'], '*bleep*',$text); 

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);

Reply With Quote
  #9  
Old October 21st, 2002, 03:56 PM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
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:
function stri_replace$find$replace$string )
    {
    
$parts explodestrtolower$find ), strtolower$string ) );

    
$pos 0;

    foreach( 
$parts as $key=>$part )
        {
        
$parts$key ] = substr($string$posstrlen($part));
        
$pos += strlen($part) + strlen($find);
        }

    return( 
join$replace$parts ) );


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:
foreach ( $words as $key=>$word )
    {
    
$replace '****';
    
$post stri_replace$word$replace$post );


Where $post holds the data you are searching through for the bad words....

Reply With Quote
  #10  
Old October 21st, 2002, 04:03 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
or juse use ereg_replace

Reply With Quote
  #11  
Old October 21st, 2002, 04:20 PM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
Or preg_replace().....

Reply With Quote
  #12  
Old October 21st, 2002, 04:27 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
or eregi_replace()...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > problems with str_replace


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway