note 31152 added to function.preg-match-all

From: Date: Fri, 11 Apr 2003 10:01:49 +0000
Subject: note 31152 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-46489@lists.php.net to get a copy of this message
Here's a regular expression to match html attributes made by me for an template class.... '/([a-zA-Z0-9]+)[\s]{0,1}=[\s]{0,1}((\')|"|)((?: (?:\\\\\\2)|(?(3)[^\']|[^"]))*)(\2)(?:\s|$)/U' (don't forget to put it on one line!!!) use it like this.... $str = 'attrName="attrV\"alue2" attrName=attrValue2 attrName=\'attr\\\'Value3\' '; //all on one line!!!! $regexp = '/([a-zA-Z0-9]+)[\s]{0,1}=[\s]{0,1}((\')|"|) ((?:(?:\\\\\\2)|(?(3)[^\']|[^"]))*)(\2)(?:\s|$)/U'; preg_match_all($regexp,$str,$matches,PREG_SET_ORDER); print_r($matches); ......will output Array ( [0] => Array ( [0] => attrName1="attrV\"alue2" [1] => attrName1 [2] => " [3] => [4] => attrV\"alue2 [5] => " ) [1] => Array ( [0] => attrName2=attrValue2 [1] => attrName2 [2] => [3] => [4] => attrValue2 [5] => ) [2] => Array ( [0] => attrName3='attr\'Value3' [1] => attrName3 [2] => ' [3] => ' [4] => attr\'Value3 [5] => ' ) ) p.s. Oostzaan rules ;-P -- http://www.php.net/manual/en/function.preg-match-all.php http://master.php.net/manage/user-notes.php?action=edit+31152 http://master.php.net/manage/user-notes.php?action=delete+31152 http://master.php.net/manage/user-notes.php?action=reject+31152

« previous php.notes (#46489) next »