简体   繁体   English

如何提取与preg_match匹配的字符串

[英]How to extract a string matching with preg_match

I have extract to class names from the html file and each line contains the class name like 我已经从html文件中提取了类名,并且每一行都包含类名,例如

      </div><div id='css-11_uss_pss_sss-__i__' class='sss'> <div class="sss-top">
      </div><div id='css-42_aap-8' class='css'> <div class="sss-top">

I need to extract the 'uss_pss_sss' and 'aap-8' from different lines 我需要从不同的行中提取“ uss_pss_sss”和“ aap-8”

I try this code it extracts the first condition. 我尝试这段代码提取第一个条件。

            preg_match("/(?<=_).*?(?=-__i__)/", $buffer, $match);

How do i extract the second line? 我如何提取第二行?

You can't for two reasons: 您不能出于两个原因:

1) aap-8 can't be a result of your pattern since it's not followed by -__i__ as indicated in your lookahead assertion 1) aap-8不能是您的模式的结果,因为前瞻性断言中未-__i__

2) the preg_match function give only the first match, you must use preg_match_all instead 2) preg_match函数只给出第一个匹配项,而必须使用preg_match_all

You can solve these two problems with: 您可以使用以下方法解决这两个问题:

preg_match_all("/(?<=_).*?(?=-(?:__i__)?|')/", $buffer, $matches);
print_r($matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM