Page 1 of 1
String to array then back to string
Posted: Sat Mar 17, 2007 8:39 pm
by GeXus
I'm trying to take a string and put it into an array (done that), now i'm looping through the array using foreach and replacing certain values (got that), now how do I put this back into a string? It's creating like a multidimential array, which is not what I want.. im kinda at a loss
Posted: Sat Mar 17, 2007 8:41 pm
by John Cartwright
Can you post your array? Most often if you want to store as an array as a string, you'd use serialize(), or for simple arrays you can use implode(). For more complex arrays, you'll likely need to build the string yourself using concantation (sp?)
Posted: Sat Mar 17, 2007 8:47 pm
by GeXus
Code: Select all
foreach($getBeginning as $word){
$original = $word;
$word = mysql_escape_string($word);
$db->sql_query("select synonym from synonyms where word = '$word' limit 1");
while($row = $db->fetch_array()){
$synonym = $row['synonym'];
$synonymWord = str_replace($word, $synonym, $synonymWord);
}
$new[$original] = $synonymWord;
}
print_r($new);
Posted: Sat Mar 17, 2007 8:48 pm
by John Cartwright
you forgot to include the array, but it looks like a simple implode() should suffice.
Posted: Sat Mar 17, 2007 8:50 pm
by GeXus
sorry
Code: Select all
$getBeginning = explode(" ", $getBeginning);
Posted: Sat Mar 17, 2007 8:51 pm
by John Cartwright
just so you don't miss it, I editted my last post.
Posted: Sat Mar 17, 2007 9:07 pm
by GeXus
Your right, it works.. i was doing some extra crap in there that wasnt neccassary.. thanks!