How to Remove <Br /> Tags and More from a String

How to remove br / tags and more from a string?

To manipulate HTML it is generally a good idea to use a DOM aware tool instead of plain text manipulation tools (think for example what will happen if you enounter variants like <br/>, <br /> with more than one space, or even <br> or <BR/>, which altough illegal are sometimes used). See for example here: http://sourceforge.net/projects/simplehtmldom/

Remove br tags from the beginning and end of a string

You could use preg_replace and a regular expression like this.

$s = '<br><br /><Br  ><br   />Hello, World!<br><br />testing 123<br  ><BR   /><br><br />';

$stripped = preg_replace('/^(<br\s*\/?>)*|(<br\s*\/?>)*$/i', '', $s);

var_dump($stripped);

Outputs:

string(57) "Hello, World!<br><br />testing 123"

How to remove the br tag from the end of a string

With substring function.

var str = "how <br> are <br> you <br>";
alert(str.substring(0, str.lastIndexOf("<br>"))); // check before if <br> is there in the end.

Removing the br / tags before the strings

Use preg_replace with the following regular expression: /^(<br\s*\/>\s*)*/.

It will remove all <br/> tags at the beginning of your message.

$str = "<br />
<br />
This message was created automatically by mail delivery software.<br />
<br />
A message that you sent could not be delivered to one or more of its<br />
recipients. This is a permanent error. The following address(es) failed:
<br />
<br />
fvsafsafsaf@shitmail.com<br />
retry timeout exceeded";

print_r(preg_replace('/^(<br\s*\/>\s*)*/', '', $str));

Output:

This message was created automatically by mail delivery software.<br />
<br />
A message that you sent could not be delivered to one or more of its<br />
recipients. This is a permanent error. The following address(es) failed:
<br />
<br />
fvsafsafsaf@shitmail.com<br />
retry timeout exceeded

What's the best way to remove br tags from the end of a string?

As @Mitch said,

//  using System.Text.RegularExpressions;

/// <summary>
/// Regular expression built for C# on: Thu, Sep 25, 2008, 02:01:36 PM
/// Using Expresso Version: 2.1.2150, http://www.ultrapico.com
///
/// A description of the regular expression:
///
/// Match expression but don't capture it. [\<br\s*/?\>], any number of repetitions
/// \<br\s*/?\>
/// <
/// br
/// Whitespace, any number of repetitions
/// /, zero or one repetitions
/// >
/// End of line or string
///
///
/// </summary>
public static Regex regex = new Regex(
@"(?:\<br\s*/?\>)*$",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
regex.Replace(text, string.Empty);

Remove BR tag from the beginning and end of a string

A couple of loops would solve the issue and be easier to read and understand (use a regex = tomorrow you look at your own code wondering what the heck is going on)

while(source.StartsWith("<br>")) 
source = source.SubString(4);
while(source.EndsWith("<br>"))
source = source.SubString(0,source.Length - 4);

return source;

php: trim br tags from the beginning of a string?

Just add an appropriate anchor (^):

preg_replace('/^(?:<br\s*\/?>\s*)+/', '', $string);

This will match multiple <br>s at the beginning of the string.

(?:…) is a non-capturing group since we only use the parentheses here to group the expression, not capture it. The modifier isn’t strictly necessary – (…) would work just as well, but the regular expression engine would have to do more work because it then needs to remember the position and length of each captured hit.

how to globally remove /br from string

you can use '\' in your regular expression to include special character of regex like '/'
here is example

const str = "<br/><br/>";
str.replace(/<\/br>/g,"else");


Related Topics



Leave a reply



Submit