4

I am looking for a way to strip all anchor tags also i want everything from ',' to <br> to be removed but <br> should remain thr.

dirty input:

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 be like:

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

please help!

-- following is the 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
  • first line to read as: I am looking for a way to strip all anchor tags also i want everything from ',' to linebreak tag to be removed but line break should remain thr.
    – ehmad11
    Dec 10, 2010 at 14:01
  • isn't any other way without using HTML Purifier? !
    – ehmad11
    Dec 10, 2010 at 14:11
  • Try my answer and see if it works for you.
    – BoltClock
    Dec 10, 2010 at 14:15
  • note: all lines are in single string..
    – ehmad11
    Dec 10, 2010 at 14:37

6 Answers 6

1

Normally it's bad to use regex to deal with HTML strings, but assuming all your links are formed like that then using preg_replace() shouldn't pose problems. Try this

// Removes all links
$str = preg_replace("/<a href=\"#([A-Z\\/]+?)\">\\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 the other answers suggesting strip_tags(): it won't erase text contained by a pair of HTML tags that it strips. For example

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

becomes

Accountant NP

which isn't quite what the OP wants.

5
  • it ignores every br till end which is not desired.. your code gives this output: Abstractor HLTH Account Representative ignoring 'Accountant' because its between "," and the last br
    – ehmad11
    Dec 10, 2010 at 14:32
  • @ehmad1: Do you mean all three lines are in a single string?
    – BoltClock
    Dec 10, 2010 at 14:33
  • @ehmad1: Give me a while more.
    – BoltClock
    Dec 10, 2010 at 14:37
  • @ehmad1: Could you paste that sprintf() part into your question? I'm not exactly going to type that all out to test...
    – BoltClock
    Dec 10, 2010 at 14:44
  • @ehmad1: I updated my code too. Can you try it and see if you get the desired output?
    – BoltClock
    Dec 10, 2010 at 14:48
1

I would strongly advise using HTML Purifier http://htmlpurifier.org/

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

0

strip-tags() for the tags, str_replace() with strpos() for the other thing.

2
  • strip_tags ONLY allow certain tags to remain, but OP want reverse way
    – ajreal
    Dec 10, 2010 at 14:05
  • Oh, yes, right. Leave the answer anyway, maybe someone is interested in it.
    – KingCrunch
    Dec 10, 2010 at 14:08
0

HTML Purifier is your friend. It has flexible options, and is very sophisticated. Doing such things with str_replace or regular expressions is wrong.

1
  • I did not downvote, but why is there an upvote for one HTML Purifier answer and a downvote for this one?
    – BoltClock
    Dec 10, 2010 at 14:14
0

strip_tags has a second argument which allows you to supply a string of allowable tags. It will strip all tags except the ones you supply:

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

This will strip everything apart from br tags.

As KingCrunch says, str_replace and strpos for the rest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.