tags:

views:

77

answers:

5
$variable = 'Afrikaans 
Shqip - Albanian  
Euskara - Basque';

How do I convert each new line to paragraph?

$variable should become:

<p>Afrikaans</p>
<p>Shqip - Albanian</p>
<p>Euskara - Basque</p>
+1  A: 
$array = explode("\n", $variable);
$newVariable = '<p>'.implode('</p><p>', $array).'</p>'
kgb
+6  A: 

Try this:

$variable = str_replace("\n", "</p>\n<p>", '<p>'.$variable.'</p>');
Tim Fountain
+1 for keeping line breaks. It's hard as hell to read HTML source without it
Col. Shrapnel
+2  A: 

The following should do the trick :

$variable = '<p>' . str_replace("\n", "</p><p>", $variable) . '</p>';
wimvds
A: 

Try:

$variable = 'Afrikaans
Shqip - Albanian  
Euskara - Basque';

$result = preg_replace("/\r\n/", "<p>$1</p>", $variable);
echo $result;
Sarfraz
A: 
<?php
$variable = 'Afrikaans  
Shqip - Albanian   
Euskara - Basque'; 
$prep0 = str_replace(array("\r\n" , "\n\r") , "\n" , $variable);
$prep1 = str_replace("\r" , "\n" , $prep0);
$prep2 = preg_replace(array('/\n\s+/' , '/\s+\n/') , "\n" , trim($prep1));
$result = '<p>'.str_replace("\n", "</p>\n<p>", $prep2).'</p>'; 
echo $result;
/*
<p>Afrikaans</p>
<p>Shqip - Albanian</p>
<p>Euskara - Basque</p>
*/
?>

Explanation:

$prep0 and $prep1: Make sure each line ends with \n.

$prep2: Remove redundant whitespace. Keep linebreaks.

$result: Add p tags.

If you don't include $prep0, $prep1 and $prep2, $result will look like this:

<p>Afrikaans  
</p>
<p>Shqip - Albanian   
</p>
<p>Euskara - Basque</p>

Not very nice, I think.

Also, don't use preg_replace unless you have to. In most cases, str_replace is faster (at least according to my experience). See the comments below for more information.

matsolof
No, I don't agree. isn't it a bit complicated for such a trifle task? and did you **really** experienced any difference between preg_replace and str_replace. I can't believe that.
Col. Shrapnel
Isn't it a bit complicated for such a trifle task? you ask. Well, that depends on whether you care about how your code is formatted or not. If you don't care and think it's ok that your code looks like in the grey box right above, don't use $prep0, $prep1 and $prep2. If you do care, like I do, and want well formatted code, use $prep0, $prep1 and $prep2.
matsolof
Did you really experience any difference between preg_replace and str_replace? you ask. Yes, I did. According to my latest benchmark (replacing all the spaces in a short text with a minus sign 10,000 times), str_replace is more than two times faster than preg_replace.
matsolof
I believe in optimizing code whenever it's possible. As long as your scripts are relatively small or doesn't handle very big strings or arrays, you won't notice a difference between optimized code and not optimized code. However, if your scripts later become bigger or have to handle bigger strings or arrays you will notice a difference.
matsolof
Also, on php.net, the official PHP site, it says on the page http://php.net/manual/en/function.str-replace.php: "If you don't need fancy replacing rules (like regular expressions), you should always use this function [str_replace] instead of ereg_replace() or preg_replace()."
matsolof
In other words, one str_replace is most likely faster than one preg_replace doing the same thing, while one preg_replace most likely is faster than several str_replace doing the same thing.
matsolof
I you still don't believe me, go to some or all of these pages: http://www.php-scripts.com/php_diary/011303.php3 --- http://www.simplemachines.org/community/index.php?topic=175031.0 --- http://ilia.ws/files/zend_performance.pdf --- http://blogs.digitss.com/php/php-performance-improvement-tips/ --- http://www.hm2k.com/posts/50-php-optimisation-tips-revisited --- Or do your own google search for "benchmark str_replace preg_replace" or something like that.
matsolof
I have merely asked you if you ever experienced such a difference of your own. In real life, not lame tests. I assume - no.
Col. Shrapnel
As a matter of fact, yes. Among others, I've written scripts to count large number of words (several millions). Using str_replace instead of preg_replace have sometimes made these scripts several seconds faster. At least as I write code. I'm no PHP expert, though.
matsolof