| | |
Replacing many newlines with 1 or 2 <br>'s
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
If I have a string like this:
What kind of regex would I use to change all single newlines (like between 'blah' and 'test') to <.br> and all multiple newlines (like between 'test' and 'asdf') to a maximum of two <.br>'s?
I've tried these already:
I'm out of ideas. Anyone know what else I can try?
PHP Syntax (Toggle Plain Text)
blah test asdf
What kind of regex would I use to change all single newlines (like between 'blah' and 'test') to <.br> and all multiple newlines (like between 'test' and 'asdf') to a maximum of two <.br>'s?
I've tried these already:
php Syntax (Toggle Plain Text)
// I had to change br tags to <.br> to make vB not remove them. I don't have a period in the tag in my actual code. // Replaces each \n with one br tag each, so huge amounts of linebreaks if huge amount of newlines $text = preg_replace("/(\n)+/m", '<.br>', $text); // Same result $text = preg_replace("/(\n)+/mU", '<.br>', $text); // Puts a <br> after every character in my string (definitely not what I wanted) $text = preg_replace("/(\n)*/m", '<.br>', $text); // Inserts no newlines $text = preg_replace("/\n+$/mU", '<.br>', $text);
I'm out of ideas. Anyone know what else I can try?
Last edited by mmiikkee12; Apr 27th, 2007 at 7:51 pm.
You can definately do that with regex, but I believe without would be faster:
eg:
[PHP]
function my_nl2br($str, $rep = "<br />", $max = 2) {
$arr = explode("\r\n", $str);
$str = '';
$nls = 0;
foreach($arr as $line) {
$str .= $line;
if (empty($line)) {
$nls++;
} else {
$nls = 0;
}
if ($nls < $max) {
$str .= $rep;
}
}
return substr($str, 0, strlen($str) - strlen($rep));
}[/PHP]
Usage:
[PHP]echo my_nl2br2($str);[/PHP]
I haven't tested it though...
eg:
[PHP]
function my_nl2br($str, $rep = "<br />", $max = 2) {
$arr = explode("\r\n", $str);
$str = '';
$nls = 0;
foreach($arr as $line) {
$str .= $line;
if (empty($line)) {
$nls++;
} else {
$nls = 0;
}
if ($nls < $max) {
$str .= $rep;
}
}
return substr($str, 0, strlen($str) - strlen($rep));
}[/PHP]
Usage:
[PHP]echo my_nl2br2($str);[/PHP]
I haven't tested it though...
![]() |
Similar Threads
- Replacing file contents (Python)
- Newlines in txt files take 2 bytes? (C)
- Ide #1 Error after replacing a dvd with an dvd/cd-rw (Storage)
- C++ HTML Encode Function (C++)
- Replacing Hard Drive (Storage)
- replace printer w/o replacing whole system? (Apple Hardware)
- Replacing a field separator during a read. (Perl)
- Old IDE board needs replacing (Storage)
- Finding and Replacing Strings (C++)
Other Threads in the PHP Forum
- Previous Thread: php tutorial
- Next Thread: need help in php
| Thread Tools | Search this Thread |
ajax apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion regex remote script search server sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validation validator variable video virus votedown web window.onbeforeunload=closeme; xml youtube





