PHP: regexp and individual tags

I am looking for a way to remove all anchor tags, and I want everything from ',' to <br> be removed, but <br> should remain the three of us.

dirty entrance:

 Abstractor HLTH<br> Account Representative, Major <a href="#P">P</a><br> Accountant <a href="#NP">NP</a>, <a href="#M">M</a>, <a href="#REA">REA</a>, <a href="#SKI">SKI</a><br> 

It should look like this:

 Abstractor HLTH<br> Account Representative<br> Accountant <br> 

Please, help!

- the following dirty text:

 $str = sprintf(' Abstractor HLTH<br> Account Representative, Major <a href="#P">P</a><br> Accountant <a href="#NP">NP</a>, <a href="#M">M</a>, <a href="#REA">REA</a>, <a href="#SKI">SKI</a><br> Accountant, Cost I & II (See Cost Accountant I, II) <a href="#FR">FR</a><br> Accountant, General <a href="#G">G</a><br> Accountant, General I (Junior) (See General Accountant) <a href="#FR">FR</a>, <a href="#O/G">O/G</a>, <a href="#W">W</a><br> Accountant, General II (Intermediate) (See General Accountant) <a href="#FR">FR</a>, <a href="#O/G">O/G</a>, <a href="#W">W</a>, <a href="#HA">HA</a> <br> Accountant, General III (Senior) (See General Accountant) <a href="#FR">FR</a>, <a href="#O/G">O/G</a>, <a href="#W">W</a> <br> '); 
+4
source share
6 answers

It’s usually bad to use a regular expression to process HTML strings, but assuming all your links are formed like this and using preg_replace() should not cause problems. try it

 // Removes all links $str = preg_replace("/<a href=\"#([AZ\\/]+?)\">\\1<\\/a>(?:, )?/i", "", $str); // Strip the comma and everything from the comma // to the next <br> in the line $str = preg_replace("/,(.*?)(?=<br>)/i", "", $str); 

To other answers suggesting strip_tags() : it will not remove text containing a pair of HTML tags that it shares. for instance

 Accountant <a href="#NP">NP</a> 

becomes

 Accountant NP 

which is not exactly what the OP wants.

+1
source

I would highly recommend using the HTML Purifier http://htmlpurifier.org/

It is fairly easy to set up, has an excellent reputation and is extremely powerful.

+1
source

strip-tags () for str_replace () tags with strpos () for another.

0
source

HTML cleaner is your friend. It has flexible options and is very sophisticated. Performing such actions with str_replace or regular expressions is incorrect .

0
source
 $clean_string = strip_tags($original_string, '<br>'); 

This will share everything except br tags.

As KingCrunch says, str_replace and strpos for the rest.

0
source

strip_tags has a second argument that allows you to supply a string of valid tags. It will share all tags except those that you supply:

 $string = strip_tags($string, '<br>'); // will leave <br>-tags in place 
0
source

All Articles