note 98336 added to function.preg-replace-callback

From: Date: Wed, 09 Jun 2010 19:01:20 +0000
Subject: note 98336 added to function.preg-replace-callback
Groups: php.notes 
Request: Send a blank email to php-notes+get-169992@lists.php.net to get a copy of this message
Created this to fetch the link and name of an anchor tag. I use this when cleaning an HTML email to text. Using regex for HTML is not recommended but for this purpose I see no issue with it. This is not designed to work for nested anchors. 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. <?php /** * 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 - <a (must have the start of the anchor tag ) * - 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.- <a href=http: //php.net>PHP.net</a> will not be replaced) */ function replaceAnchorsWithText($data) { /** * Had to modify $regex so it could post to the site... so I broke it into 6 parts. */ $regex = '/(<a\s*'; // Start of anchor tag $regex .= '(.*?)\s*'; // Any attributes or spaces that may or may not exist $regex .= 'href=[\'"]+?\s*(?P<link>\S+)\s*[\'"]+?'; // Grab the link $regex .= '\s*(.*?)\s*>\s*'; // Any attributes or spaces that may or may not exist before closing tag $regex .= '(?P<name>\S+)'; // Grab the name $regex .= '\s*<\/a>)/i'; // Any number of spaces between the closing anchor tag (case insensitive) if (is_array($data)) { // This is what will replace the link (modify to you liking) $data = "{$data['name']}({$data['link']})"; } return preg_replace_callback($regex, 'replaceAnchorsWithText', $data); } $input = 'Test 1: <a href="http: //php.net1">PHP.NET1</a>.<br />'; $input .= 'Test 2: <A name="test" HREF=\'HTTP: //PHP.NET2\' target="_blank">PHP.NET2</A>.<BR />'; $input .= 'Test 3: <a hRef=http: //php.net3>php.net3</a><br />'; $input .= 'This last line had nothing to do with any of this'; echo replaceAnchorsWithText($input).'<hr/>'; ?> 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 ---- Server IP: 69.147.83.197 Probable Submitter: 63.103.168.2 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php Edit -- https://master.php.net/note/edit/98336 Del: integrated -- https://master.php.net/note/delete/98336/integrated Del: useless -- https://master.php.net/note/delete/98336/useless Del: bad code -- https://master.php.net/note/delete/98336/bad+code Del: spam -- https://master.php.net/note/delete/98336/spam Del: non-english -- https://master.php.net/note/delete/98336/non-english Del: in docs -- https://master.php.net/note/delete/98336/in+docs Del: other reasons-- https://master.php.net/note/delete/98336 Reject -- https://master.php.net/note/reject/98336 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169992) next »