Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: remove excess <br> and tags from a string

Tags:

php

how can I remove excess <br> and &nbsp; tags from the start and end of a string?

Thanks :)

like image 382
user90501 Avatar asked Apr 14 '09 02:04

user90501


People also ask

How do you remove a BR tag from a string?

The line break can be removed from string by using str_replace() function.

How to Remove a tag using PHP?

PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.

Can we remove BR tag using CSS?

Solution: It is not possible to remove just <p> tags without removing content inside.


1 Answers

try this:

$str = preg_replace('{^(<br(\s*/)?>|&nbsp;)+}i', '', $str); //from start
$str = preg_replace('{(<br(\s*/)?>|&nbsp;)+$}i', '', $str); //from end

that also gets XHTML <br /> and <br/> forms

like image 129
Kip Avatar answered Oct 25 '22 01:10

Kip