Reputation: 1306
Quick question:
What's the best way to go about removing <br>
and <br />
tags from the start and end of a $string
?
I'm currently using this code, but it doesn't appear to remove just <br>
tags.
$str = preg_replace('{^(<br(\s*/)?>|ANDnbsp;)+}i', '', $str);
$str = preg_replace('{(<br(\s*/)?>|ANDnbsp;)+$}i', '', $str);
EDIT: Additional Information
This code is dealing with information that has been imported from an old CMS.
As such, I know that the only two tags I need to replace are <br>
and <br />
. Additionally, I am only looking to replace these tags at the very beginning and very end of a $string
, not in between.
I don't need to deal with any other tags; malformed HTML nor additional attributes.
Essentially I would simply like to expand the code I have suggested so that the <br>
tags are replaced as well as <br />
.
I apologise for not offering enough information to begin with.
Thanks in advance,
Upvotes: 3
Views: 4833
Reputation: 10234
One possibility of regex is this:
"/(^)?(<br\s*\/?>\s*)+$/"
So let's make it clearer:
$str = preg_replace("/(^)?(<br\s*\/?>\s*)+$/", "", $str);
Explaining:
A good starting point: Regular expressions in Perl
Upvotes: 3
Reputation: 12925
Thou shall not use Regex to parse html.
If you get a regex that matches <br />
and <br>
, what happens if someone throws in a style
, class
, or id
? If you write that, what if they throw in a title? or just put in some badly formatted code?
You should use a function like strip_tags()
here.
Or a DOM parser here.
Upvotes: 2