Jump to content

Putting a string into an array


aTeam

Recommended Posts

Hi All,

 

I am trying to get a string from a bad word filter into an array so i can sort the data then email it.

 

i.e. after a Bad word filter it leaves my data full of gaps so i believe if i can sort the data in alphabetical order it will remove all these gaps.

 

everything works except the sort function and i don't understand why, i think i am pretty close what do you guys think?

 

$sql = 'SELECT * FROM `jos_facileforms_subrecords`'
        . ' WHERE element = 158 LIMIT 0, 30 ';

// execute SQL query and get result  
$sql_result = mysql_query($sql,$connection)  
     or die(mysql_error());   

// Loop through the data set and extract each row in to it's own variable set   
  $names = "";
while ($row = mysql_fetch_array($sql_result)) { 
    $names .= $row['value'] . "\n" ;
}  

function filter($string) {
$badwords=array("ass","babi");
for ($i=0;$i < count($badwords);$i++){      
   $string = str_replace($badwords[$i],str_repeat("",strlen($badwords[$i])),$string);     
}      
return $string; 
} 


$filtered_output = filter($names);

$names_array= explode(" ", $filtered_output);

sort($names_array);

// Email results

$to = "[email protected]";
$subject = "XXXX";

$body = "$names_array";

 

-Adam

Link to comment
https://forums.phpfreaks.com/topic/145741-putting-a-string-into-an-array/
Share on other sites

add in the filter loop something like

$string = str_replace("  ", " ", $string);

that'll replace double spaces with a single space.

btw, your filter will currently make "assassin" into "in"  ;D

Both problems may be solved by changing your array to be

$badwords=array(" ass "," babi ");

or you regular expressions.

Hi,

 

lol yeah i have been through so much documentation it hurts, but i have picked up a few things and i have changed my tac.

 

Instead of filtering after the data has been extracted i think it would be better to directly replace "bad words" in the database that way you can just use the "ORDER BY" function, instead of trying to sort a string.

 

But that brings me to my next problem, i cant seem to pass the array into the mySQL query properly. The query works when i hard code it (i.e. write what i want to to find directly in the code) but it wont accept the value from the array of bad words.

 

I am sure this is a syntax thing to do with the LIKE function but i have been looking for ages and i keep failing.

 

what u guys think?

 

 

 // array of bad words 
$badwords=array("ass","babbi");
foreach ($badwords as $b)
{      
//Fileter SQL
$filter_sql = 'UPDATE jos_facileforms_subrecords SET value = \'XXX\''
		. ' WHERE value LIKE \'"%.$b.%"\''; 

// execute $filter_sql query and get result  
$filter_sql_result = mysql_query($filter_sql,$connection)  
	or die(mysql_error());   

}      

 

-Adam

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.