hi i have a little problem, i want to add a text on the begin and on the end of each line of a textarea:
test1,test11,test111,
test2,test22,test222,
test3,test33,test333,
so i write this code to add "first" on the begin on each line, but i don't know how to replace the last comma (,) from each line with a text, my code
$s=(explode(" ",str_replace("
","First",$_POST['text'])));
foreach($s as $i=> $ss){
$s[$i]=$ss."<br>";
echo "First".$s[$i];
}
If you want to just remove the comma, use substr
$ss = substr($ss,0,-1);
Then add the end and start.
probably you can do this:
$str = 'test1,test11,test111,';
echo substr_replace($str, ",Last", -1);
This code will replace the last character in the string with the text you want to replace with ... in this case I have put ",Last".. you may add any text.
here is a simple to understand answer to your question
$lines = explode("
", $_POST['text']); // make an array in which every item is a line
// modifying the lines
foreach($lines as $index => $text) {
$text = trim($text); // remove any space at the begining or end of the line
$text = substr($text, 0, strlen($text) - 1); // remove the last character from the line (which is supposed to be the ',')
$text = "First " . $text . " Last"; // adding first and last part you want
$lines[$index] = $text; // appling modification of the array $lines
echo $lines[$index] . "<br>"; // showing the line !
}