PDA

View Full Version : PHP str_replace - should be easy!



thedaveyb
September 12th, 2008, 05:30 AM
Yo Kirupians,

I am try to split a string that comes from the database - its currently a comma i wish to replace with a break -

('Northwich, Cheshire' becomes 'Northwich<br>Cheshire')



$row['Address']= str_replace(", ","<br>", $row['Address']);


then this is to be used below when an email is sent..



$body .= $row['Address'].",<br>\n";


However it is just coming up as blank in the email! Any ideas on where I am going wrong!?

tfg
September 12th, 2008, 06:40 AM
personally i'd assign the address to a new variable instead of modifying the value in the database output array.

$address = str_replace(", ", "<br />", $row['Address']);

$body .= $address . ",<br />\n";

what's the other code around that? is there anything that could be affecting it? could it even be that there's an error in your script just above where you're setting the address and it's not executing that part? is there even the data in the database!?

use print_r($row); to output the contents of the $row array to your browser. it might give you some clues.

thedaveyb
September 12th, 2008, 06:51 AM
brilliant thats done the trick - for info 2 school boy errors!

1. As you say assign to a variable, I had tried this approach but I had done it this way round... doh!



$row['Address']= str_replace(", ", "<br />", $address );


2. As you say also, it was being affected by other code, I had placed the replace too far up, placing just above the variable i wanted changing had the desired affect!.

ta muchly.

tfg
September 12th, 2008, 06:54 AM
you're welcome