note 92312 added to function.preg-match-all

From: Date: Sat, 18 Jul 2009 22:51:12 +0000
Subject: note 92312 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-158462@lists.php.net to get a copy of this message
Using preg_match_all I made a pretty handy function. <?php function reg_smart_replace($pattern, $replacement, $subject, $replacementChar = "$$$", $limit = -1) { if (! $pattern || ! $subject || ! $replacement ) { return false; } $replacementChar = preg_quote($replacementChar); preg_match_all ( $pattern, $subject, $matches); if ($limit > -1) { foreach ($matches as $count => $value ) { if ($count + 1 > $limit ) { unset($matches[$count]); } } } foreach ($matches[0] as $match) { $rep = ereg_replace($replacementChar, $match, $replacement); $subject = ereg_replace($match, $rep, $subject); } return $subject; } ?> This function can turn blocks of text into clickable links or whatever. Example: <? reg_smart_replace(EMAIL_REGEX, '<a href="mailto:$$$">$$$</a>', $description) ?> will turn all email addresses into actual links. Just substitute $$$ with the text that will be found by the regex. If you can't use $$$ then use the 4th parameter $replacementChar ---- Server IP: 64.71.164.2 Probable Submitter: 216.229.21.4 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- https://master.php.net/note/edit/92312 Del: integrated -- https://master.php.net/note/delete/92312/integrated Del: useless -- https://master.php.net/note/delete/92312/useless Del: bad code -- https://master.php.net/note/delete/92312/bad+code Del: spam -- https://master.php.net/note/delete/92312/spam Del: non-english -- https://master.php.net/note/delete/92312/non-english Del: in docs -- https://master.php.net/note/delete/92312/in+docs Del: other reasons-- https://master.php.net/note/delete/92312 Reject -- https://master.php.net/note/reject/92312 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158462) next »