php - regex in preg_match_all() doesn't work as expected -


i have following string:

"<h2>define vim greatest</h2> word processor, <h3>vi</h3>!". 

i want select h2 , h3 following structure regex.

expected output be:

array(     0   =>  <h2>define vim greatviest</h2>     1   =>  <h3>vi</h3> ) 

so implement regular expression follow:

preg_match_all("/(?:<h2>|<h3>).*vi.*(?:<\/h2>|<\/h3>)/i", $input, $matches) 

but instead of desirable result above, outputs following result.

current output:

array(     0 => <h2>define vim greatviest</h2> word prviocessor ever created <h3>vi</h3> ) 

how can change code/regex, tags in expected output above?

well problem is, first missing delimiters regex , second vi case-sensitive, have add i flag, case-insensitivity.

so code (just removed vi in regex , grab between h1-6 tags):

<?php      $input = '"<h2>define vim greatest</h2> word processor, <h3>vi</h3>!".';      preg_match_all("/(?:<h[0-6]>).*?(?:<\/h[0-6]>)/", $input, $matches);     print_r($matches);  ?> 

output:

array (     [0] => array         (             [0] => <h2>define vim greatest</h2>             [1] => <h3>vi</h3>         )  ) 

edit:

as updated regex problem is, .* greedy, means takes as can. make non-greedy have add ? @ end. change .* -> .*?.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -