note 102285 added to regexp.reference.conditional
| From: | Anonymous at osu1 dot php dot net | Date: | Sun, 06 Feb 2011 17:26:07 +0000 |
| Subject: | note 102285 added to regexp.reference.conditional | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176716@lists.php.net to get a copy of this message | ||
Repetition of a subpattern will repeat conditionals that are contained inside it, updating
subpattern matches with iteration.
Consider the following code, which scans thru HTML, keeping track of angle brackets "<"
">". If open bracket "<" matches, then closing bracket ">"
must follow before repetition can possibly end. That way regex will effectively match only outside
of tags.
<?php
$pattern='%(*ANY)(.*?(<)(?(2).*?>)(.*?))*?\'\'%s';
$replace='\1Fred';
$subject=
'<html><body class=\'\'>\'\' went to '\'\meyer and
ran
into <b>\'\'</b>.
</body></html>'
echo preg_replace($pattern,$replace,$subject);
?>
Output will be:
'<html><body class=\'\'>Fred went to Fredmeyer and ran
into <b>Fred</b>.
</body></html>'
----
Server IP: 69.147.83.197
Probable Submitter: 24.113.79.192
----
Manual Page -- http://www.php.net/manual/en/regexp.reference.conditional.php
Edit -- https://master.php.net/note/edit/102285
Del: integrated -- https://master.php.net/note/delete/102285/integrated
Del: useless -- https://master.php.net/note/delete/102285/useless
Del: bad code -- https://master.php.net/note/delete/102285/bad+code
Del: spam -- https://master.php.net/note/delete/102285/spam
Del: non-english -- https://master.php.net/note/delete/102285/non-english
Del: in docs -- https://master.php.net/note/delete/102285/in+docs
Del: other reasons-- https://master.php.net/note/delete/102285
Reject -- https://master.php.net/note/reject/102285
Search -- https://master.php.net/manage/user-notes.php