Explanation for PHP preg_match_all matches array print_r -
i have following usage preg_match_all
:
$re = "/^(-\\w+\\s*.*)\\n*$/um"; $str = "-give on result\n-second new text\n-the third text\n\nanother paragraph without list.\n\n-new list here"; preg_match_all($re, $str, $matches);
the output of print_r($matches)
has weird array:
array ( [0] => array ( [0] => -give on result [1] => -second new text [2] => -the third text [3] => -new list here ) [1] => array ( [0] => -give on result [1] => -second new text [2] => -the third text [3] => -new list here ) )
here i'm talking array of index 0
, visually blank line appeared in print_r()
output. indeed, question may related my question regex , need know why 2 arrays different? online demo found here
try easy make that
<?php //$re = "/^(-\\w+\\s*.*)\\n*$/um"; $re = "/^(\\h*(?:-|&)\\h*\w+\\s*.*)\\n*$/um"; $str = "-give on result\n-second new text\n-the third text\n\nanother paragraph without list.\n\n-new list here"; preg_match_all($re, $str, $matches); echo "<pre>"; print_r($matches); echo "</pre>"; $i = 0; echo "<ul>".preg_replace_callback($re,function($mt){return "<li>".$mt[1]."</li>";},$str)."</ul>"; $re = array_filter(preg_split('/\n/', $str)); echo "<pre>"; print_r($re); echo "</pre>";
Comments
Post a Comment