note 73955 added to function.preg-match
| From: | legolas558d0tusersdotsfdotnet at osu1 dot php dot net | Date: | Sat, 17 Mar 2007 17:55:38 +0000 |
| Subject: | note 73955 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124539@lists.php.net to get a copy of this message | ||
I wanted to match all php tags in a string but I found preg_match_all not to behave correctly:
<?php
// additional spacing added not to confuse markup highlighters
preg_match_all('/<\\?'.'php.*\\?'.'>/i', $content, $m);
?>
Returned only the single line php tags:
[0]=>
array(4) {
[0]=>
string(23) "<?php doSomething(); ?>"
[1]=>
string(19) "<?php echo 'something'; ?>"
[2]=>
string(34) "<?php doSomethingElse();?>"
[3]=>
string(35) "<?php echo 'something else'; ?>"
}
So I gave up and wrote this small function:
<?php
function php_tags($s) {
// retrieve all PHP tags from $s
// PHP tags gatherer by legolas558
// initialize variables
$offset = 0;
$tags = array();
// loop till php tags are found
while (false !== ($p = strpos($s, '<'.'?php', $offset))) {
$p += 6;
$et = strpos($s, '?'.'>', $p);
// could not find termination tag
if ($et===false)
break;
// add tag to array
$tags[] substr($s, $p, $et-$p);
// properly increment offset
$offset = $et+2;
}
return $tags;
}
?>
This function does the job, however if you have used strings containing "<?php" and
"?>" in your PHP code tags it will not work.
----
Server IP: 213.92.11.19
Probable Submitter: 151.47.93.77
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- https://master.php.net/note/edit/73955
Del: integrated -- https://master.php.net/note/delete/73955/integrated
Del: useless -- https://master.php.net/note/delete/73955/useless
Del: bad code -- https://master.php.net/note/delete/73955/bad+code
Del: spam -- https://master.php.net/note/delete/73955/spam
Del: non-english -- https://master.php.net/note/delete/73955/non-english
Del: in docs -- https://master.php.net/note/delete/73955/in+docs
Del: other reasons-- https://master.php.net/note/delete/73955
Reject -- https://master.php.net/note/reject/73955
Search -- https://master.php.net/manage/user-notes.php