Home  >  Article  >  Backend Development  >  php中Warning: preg_match_all(): Compilation failed: lookbehind assertion

php中Warning: preg_match_all(): Compilation failed: lookbehind assertion

WBOY
WBOYOriginal
2016-07-25 08:46:432094browse
Today when I was testing the code related to looking around in regular expressions, I found an error
The error message is as follows:
Warning: preg_match_all(): Compilation failed: lookbehind assertion is not fixed length at offset
The code is as follows:
  1. $str='
    info
    strong';
  2. preg_match_all('/(?<=<(w+)[ ^>]*>).*(?=)/',$str,$match);
  3. var_dump($match);
Copy the code
Execute the above code An error will be reported. After checking online, it is said that reverse lookaround in PHP (including reverse positive lookaround?<=exp, reverse negative lookaround?
Change the above code to:
  1. $str='
    info
    strong';
  2. preg_match_all('/(?<=< (w)>).*(?=)/',$str,$match);
  3. var_dump($match);
Copy code
But this can only match b Strong content in tags
Actually, you don’t need to look around to match, you can directly use grouping to capture the content. You just need to filter the returned results
  1. $str='
    info
    strong';
  2. preg_match_all('/(<(w+)[^>]*>)(.*)()/',$str,$match);
  3. print_r( $match[3]);//Array ( [0] => info [1] => strong )
Copy code




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn