How to Convert Multiple <Br/> Tag to a Single <Br/> Tag in PHP

How to Regex-replace multiple br / tags with one br / tag?

You could use s/(<br \/>)+/<br \/>/, but if you are trying to use regex on HTML you are likely doing something wrong.

Edit: A slightly more robust pattern you could use if you have mixed breaks:

/(<br\ ?\/?>)+/

This will catch <br/> and <br> as well, which might be useful in certain cases.

php replace multiple br-tags

You can use this regex:

$result = preg_replace('/(?:\s*<br[^>]*>\s*){3,}/s', "<br><br>", $input);
//=> <br><br>High Quality Print<br><br>Data<br><br>

RegEx Demo

How to convert multiple br/ tag to a single br/ tag in php

You can do this with a regular expression:

preg_replace("/(<br\s*\/?>\s*)+/", "<br/>", $input);

This if you pass in your source HTML, this will return a string with a single <br/> replacing every run of them.

Preg Match multiple br tags

You can use the following regular expression. Since the repeating tags could be on the same line or separated by a newline sequence, you need to account for whitespace.

$comment = preg_replace('~(?:<br />\s*){3,}~', '<br />', $comment);

Regex Explanation | Code Demo

If for some logical reason it removes certain whitespace you want retained, I would use ...

$comment = preg_replace('~(?:<br />\R?){3,}~', '<br />', $comment);

converting BR tags into new lines in a textarea

Probably your HTML is

foo<br>
bar<br>
nuts<br>

So you already have "\n" and replacing br to "\n" you ends up with double "\n" like

foo \n\n bar \n\n nuts

it's looks like

foo

bar

nuts

For receiving output you suggest - you need to remove "\n" from the input HTML and then replace br to "\n"

The code would be

function br2nl($st){
$st_no_lb = preg_replace( "/\r|\n/", "", $st );
return preg_replace('/<br(\s+)?\/?>/i', "\n", $st_no_lb);
}

Strip tags and replace all br and p tags with a single space

This is what I would do:

$a = '<h1>Heading</h1>
<br>
<br />
<a href="#">hyperlink</a>
<p></p>
<p>paragraph1</p>
<p>paragraph2</p>';

echo trim(preg_replace(['/<[^>]*>/','/\s+/'],' ', $a));

Output

 Heading hyperlink paragraph1 paragraph2 

Sandbox

The first regex removes tags replacing them with a space, the second one takes multiple spaces and changes it to one.

This works pretty good, but I can see a way that it could deviate from what was specifically requested.

What is the regex to strip all html tags and where there are <br> and <p> tags replace with a single space and remove all line breaks

So if you want the "full" solution, you can do this:

$a = '<h1>Heading</h1>
<br>
<br />
<a href="#">hyperlink</a>
<p></p>
<p><big>p</big>aragraph1</p><p>paragraph2</p>';

echo preg_replace([
'/<(?:br|p)[^>]*>/i', //replace br p with ' '
'/<[^>]*>/', //replace any tag with ''
'/\s+/', //remove run on space
'/^\s+|\s+$/' //trim
],[
' ', '', ' ', ''
], $a);

Please note i added a <big> tag in and removed any space between the <p> tags. These were done to highlight a few things.

For example if you take the text from the second example and use it in the first you will get this (because the big tag):

Heading hyperlink p aragraph1 paragraph2 

The updated example outputs correctly. But, and this is a big but, I changed the input text, so it may not be necessary to over-complicate it.

The <p> tag thing just shows that it puts space in between them before removing all the HTML tags with ''.

Sandbox

UPDATE

@ArtisticPhoenix how would I accomodate <p> </p>

First I would convert the string using html_entity_decode however there are a few sticky points with that. These have to do with encoding. So this is the correct way to do it:

$a = '<h1>Heading</h1>
<br>
<br />
<a href="#">hyperlink</a>
<p> </p>
<p><big>p</big>aragraph1</p><p>paragraph2</p>';

//convert entities using UTF-8
$a = html_entity_decode($a, ENT_QUOTES, 'UTF-8');

echo preg_replace([
'/<(?:br|p)[^>]*>/i', //replace br p with ' '
'/<[^>]*>/', //replace any tag with ''
'/\s+/u', //remove run on space - replace using the unicode flag
'/^\s+|\s+$/u' //trim - replace using the unicode flag
],[
' ', '', ' ', ''
], $a);

Please note the addition of the u flag to the regex above /\s+/u and /^\s+|\s+$/u.

u (PCRE_UTF8)
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern and subject strings are treated as UTF-8. An invalid subject will cause the preg_* function to match nothing; an invalid pattern will trigger an error of level E_WARNING. Five and six octet UTF-8 sequences are regarded as invalid since PHP 5.3.4 (resp. PCRE 7.3 2007-08-28); formerly those have been regarded as valid UTF-8.

The problem comes from decoding it into a ASCII 160 (nbsp) instead of ASCII 32 character (single space). Anyway we can use UTF-8 to sort it out as shown above.

Sandbox

How to convert multiple br tags to single br tag but without spaces

Here you are regex to convert the br tags :)

Example here http://jsfiddle.net/3Ldqhuae/

var str = $("#content").html(), 
regex = '#(<br */?>\s*)+#i';

$("#content").html( str.replace( regex , '<br />') );

Trying to replace br, BR, br +attribute tags with br/

This regex will do what you want: <(BR|br)[^>]*>

Here is a working example: Regex101

Reduce multiple br tags using regex in JavaScript

Your error is in the server code. From the PHP manual on readfile: Reads a file and writes it to the output buffer. [...] Returns the number of bytes read from the file.

You are storing the XML's length in $xdef, not the string content. Use file_get_contents instead of readfile and you'll be fine. No use for (indeed very complicated) client-side corrections.



Related Topics



Leave a reply



Submit