I have been working on a email system that will automatically generate a text email from a given HTML email by using strip_tags().
The only issue I ran into, for my needs, were that the anchors would not keep their links.
I search for a little while and could not find anything to strip the links from the tags so I generated my own little snippet.
I am posting it here in hopes that others may find it useful and for later reference.
A note to keep in mind:
I was primarily concerned with valid HTML so if attributes do no use ' or " to contain the values then this will need to be tweaked.
If you can edit this to work better, please let me know.
* Replaces anchor tags with text
* - Will search string and replace all anchor tags with text (case insensitive)
*
* How it works:
* - Searches string for an anchor tag, checks to make sure it matches the criteria
* Anchor search criteria:
* - 1 -
* - 2 - Can have any number of spaces or other attributes before and after the href attribute
* - 3 - Must close the anchor tag
*
* - Once the check has passed it will then replace the anchor tag with the string replacement
* - The string replacement can be customized
*
* Know issue:
* - This will not work for anchors that do not use a ' or " to contain the attributes.
* (i.e.- will not be replaced)
*/functionreplaceAnchorsWithText($data) {/**
* Had to modify $regex so it could post to the site... so I broke it into 6 parts.
*/$regex='/(
}
returnpreg_replace_callback($regex,'replaceAnchorsWithText',$data);
}$input='Test 1: .
';$input.='Test 2: .
';$input.='Test 3:
';$input.='This last line had nothing to do with any of this';
echoreplaceAnchorsWithText($input).'
';?>Will output:Test 1: PHP.NET1(http: //php.net1).
Test 2: PHP.NET2(HTTP: //PHP.NET2).
Test 3: php.net3 (is still an anchor)
This last line had nothing to do with any of this
Posting to this site is painful...
Had to break up the regex and had to break the test links since it was being flagged as spam...