Replace coma (,) in text field with a newline break

Hi i have a textarea box that user enters list of names what i’ve been trying to do is when retriving the data from the database the specific row .$row[“name_drivers”].
when display back the data evrytime it finds a coma in that field replaces with a line break e.g
gary nevile, steven gerard, michael tyson, david ginola
instead of displaying like this displays like above
gary nevile
steven gerard
michael tyson
david ginola

i have tried

$name_drivers = str_replace(“,”, "
", $name_drivers);
but no luck is it possible to achive somthing what i am trying to do
here my whole code

$name_drivers = str_replace("<br>", "\
", $name_drivers);  
 $id = $_GET['c'];
 
 $drivers = mysql_query("SELECT name,  date_of_birth, start_time, number_drivers, name_drivers, last_update, display,  DATE_FORMAT(date_of_birth, '%a %D %b %Y')  as new_date,  DATE_FORMAT(last_update, '%a %D %b %Y')  as new_update, DATE_FORMAT(start_time, '%H:%i') as timing FROM event WHERE id='$id' ");


 while ($row = mysql_fetch_assoc($drivers)) {
  if(isset($row['display']) && $row['display'] == "1") {

     echo "<p><strong>Track:</strong> ".$row["name"]."</p>";
     echo "<p><strong>Track:</strong> ".$row["name"]."</p>";
     echo "<p><strong>Date:</strong> ".$row["new_date"]."</p>";
     echo "<p><strong>Start Time:</strong> ".$row["timing"]."am</p>";
     echo "<p><strong>Number of drivers booked in:</strong> ".$row["number_drivers"]."</p>";
         echo "<p><strong>Last Update:</strong> ".$row["new_update"]."</p>";
         echo "<p><strong>List of drivers booked in:</strong> ".$row["name_drivers"]."</p>";
       
    }

Because
isnt a line break as far as HTML output is concerned. Try replacing ‘,’ with ‘<br />’ instead.

I tried and i get the follow error

Notice: Undefined variable: name_drivers in C:\xampp\htdocs\events\event_details.php on line 18

which is this line
$name_drivers = str_replace(“,”, “<br>”, $name_drivers);

That means $name_drivers isnt defined. Where do you set $name_drivers before that line?

I dont has that comes from the database and i only use the select from database and the mysql fetch
it didnt ask me to define until i put the this line
$name_drivers = str_replace(“,”, “<br>”, $name_drivers);


$name_drivers = str_replace(",", "<br>", $name_drivers);  
 $id = $_GET['c'];
  
 $drivers = mysql_query("SELECT name,  date_of_birth, start_time, number_drivers, name_drivers, last_update, display,  DATE_FORMAT(date_of_birth, '%a %D %b %Y')  as new_date,  DATE_FORMAT(last_update, '%a %D %b %Y')  as new_update, DATE_FORMAT(start_time, '%H:%i') as timing FROM event WHERE id='$id' ");


 while ($row = mysql_fetch_assoc($drivers)) {
  if(isset($row['display']) && $row['display'] == "1") {

     echo "<p><strong>Track:</strong> ".$row["name"]."</p>"; 
     echo "<p><strong>Track:</strong> ".$row["name"]."</p>"; 
     echo "<p><strong>Date:</strong> ".$row["new_date"]."</p>"; 
     echo "<p><strong>Start Time:</strong> ".$row["timing"]."am</p>"; 
     echo "<p><strong>Number of drivers booked in:</strong> ".$row["number_drivers"]."</p>";
	 echo "<p><strong>Last Update:</strong> ".$row["new_update"]."</p>";
	 echo "<p><strong>List of drivers booked in:</strong> ".$row["name_drivers"]."</p>";
	
    }

So… i think i understand you. And i think you want to do this instead.


     echo "<p><strong>List of drivers booked in:</strong> ".str_replace(',','<br />',$row["name_drivers"])."</p>";

works perefect Thank you very much

Hi again i was just wondering is there a way that i can count the number of comma found and then display the number found

[FPHP]substr_count[/FPHP]

Thanks for the link

i tried

echo "<p><strong>List of drivers booked in:</strong> ".str_replace(',','<br />',$row["name_drivers"])."</p>";
echo strlen($str_replace);

but got the error
Notice: Undefined variable: str_replace in C:\xampp\htdocs\events\details.php on line 34
0

well #1: strlen was not the command i pointed you towards.
#2: str_replace is not defined as a variable, its a function. If you want to define a variable to hold the changed string…


$nd_replaced = str_replace(',','<br />',$row["name_drivers"]);
echo "<p><strong>List of drivers booked in:</strong> ".$nd_replaced."</p>"; 
echo strlen($nd_replaced);  

one problem that is count the characters instead of the comma(,) found in the field

which is why i pointed you to substr_count, not strlen…

ohh ok sorry i scrolled down the page you sent me so was looking at wrong thing
trying to understand how the substr_count thanks for the direction
will let you know if i managed anything thanks once again

ok i think i understood how the substr_count works

e.g.
echo substr_count(“Hello world. The world is nice”,“world”);
echo will be 2 because it founds the word world twice

on here is trying to find the variable nd_replaced which are the commas

$nd_replaced = str_replace(',','<br />',$row["name_drivers"]);
echo "<p><strong>List of drivers booked in:</strong> ".$nd_replaced."</p>"; 
echo substr_count($nd_replaced);

but i get the error
substr_count() expects at least 2 parameters, 1 given in C:\xampp\htdocs\events\details.php on line 58

And how does substr_count know what you’re looking for? You havent told it what to look for.

yeah found it

echo substr_count("$nd_replaced",$nd_replaced ); 

it worked
last help on the count thing i am actually trying to count the words e.g.
StarLion, macaela cute, StarLion

so echo 3 results from the textbox

i thought before i could do that by counting the comma but i realised thats not what i need counting so

StarLion, macaela cute, StarLion

so echo 3 results from the textbox

Actually didnt work

haystack, needle.

substr_count($nd_replaced,“somethingyoursearchingfor”)

Hint: You’re not looking for commas anymore…

i tried like this but still no luck
my work
echo substr_count($nd_replaced, “.$nd_replaced.”);

simple echo
echo substr_count(“Hello world. The world is nice”,“world”);

this how i am trying to interpreting from the simple echo and my work. colours