how php function preg_match_all outputs matches array -


<?php // \\2 example of backreferencing. tells pcre // must match second set of parentheses in regular expression // itself, ([\w]+) in case. backslash // required because string in double quotes. $html = "<b>bold text</b><a href=howdy.html>click me</a>";  preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, preg_set_order);  foreach ($matches $val) {     echo "matched: " . $val[0] . "\n";     echo "part 1: " . $val[1] . "\n";     echo "part 2: " . $val[2] . "\n";     echo "part 3: " . $val[3] . "\n";     echo "part 4: " . $val[4] . "\n\n"; } ?> 

in example first finds matches using pattern , put them inside matches array. not understand how finds parts(val[0],val[1],......)(basically no idea part)

quite simple: index 0 holds entire string matched and, 1 n, have various capturing groups defined.

so, given regex /(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/, group 1 contain whole opening tag, group 2 specific tag name, group 3 tag content , group 4 closing tag.

for example:

$html = "<b>bold text</b>";  preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, preg_set_order);  // here have // $matches[0] == "<b>bold text</b>"; // $matches[1] == "<b>"; // $matches[2] == "b"; // $matches[3] == "bold text"; // $matches[4] == "</b>"; 

as always, see the docs:

if matches provided, filled results of search. $matches[0] contain text matched full pattern, $matches[1] have text matched first captured parenthesized subpattern, , on.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -