note 117352 added to function.preg-match-all

From: Date: Tue, 26 May 2015 15:40:12 +0000
Subject: note 117352 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-202669@lists.php.net to get a copy of this message
// Here is function that allows you to preg_match_all array of patters function getMatches($pattern, $subject) { $matches = array(); if (is_array($pattern)) { foreach ($pattern as $p) { $m = getMatches($p, $subject); foreach ($m as $key => $match) { if (isset($matches[$key])) { $matches[$key] = array_merge($matches[$key], $m[$key]); } else { $matches[$key] = $m[$key]; } } } } else { preg_match_all($pattern, $subject, $matches); } return $matches; } $patterns = array( '/<span>(.*?)<\/span>/', '/<a href=".*?">(.*?)<\/a>/' ); $html = '<span>some text</span>'; $html .= '<span>some text in another span</span>'; $html .= '<a href="path/">here is the link</a>'; $html .= '<address>address is here</address>'; $html .= '<span>here is one more span</span>'; $matches = getMatches($patterns, $html); print_r($matches); // result is below /* Array ( [0] => Array ( [0] => <span>some text</span> [1] => <span>some text in another span</span> [2] => <span>here is one more span</span> [3] => <a href="path/">here is the link</a> ) [1] => Array ( [0] => some text [1] => some text in another span [2] => here is one more span [3] => here is the link ) ) */ ---- Server IP: 72.52.91.14 Probable Submitter: 46.72.180.19 ---- Manual Page -- http://php.net/manual/en/function.preg-match-all.php Edit -- https://master.php.net/note/edit/117352 Del: integrated -- https://master.php.net/note/delete/117352/integrated Del: useless -- https://master.php.net/note/delete/117352/useless Del: bad code -- https://master.php.net/note/delete/117352/bad+code Del: spam -- https://master.php.net/note/delete/117352/spam Del: non-english -- https://master.php.net/note/delete/117352/non-english Del: in docs -- https://master.php.net/note/delete/117352/in+docs Del: other reasons-- https://master.php.net/note/delete/117352 Reject -- https://master.php.net/note/reject/117352 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202669) next »