简体   繁体   English

PHP-正则表达式-捕获多个标签内的字符串

[英]php - regex - catch string inside multiple tags

still on regex! 仍在正则表达式上! ;-))) ;-)))

Assuming we have an html file with a lot of <tr> rows with same structure like this below, where (.*?) is the content i need to extract! 假设我们有一个HTML文件,其中包含许多<tr>行,其结构如下所示,其中(.*?)是我需要提取的内容!

<tr align=center><th width=5%><a OnClick="(.*?)"href=#>(.*?)</a><td width=5%>(.*?)<td width=5% align=center >(.*?)</td></tr>

UPDATED 更新

maybe with a nice preg_match_all() ? 也许有一个不错的preg_match_all()

i need something like this result 我需要这样的结果

match[0] . match[1] . match[2] . match[3]

just in case someone need someting similar! 万一有人需要类似的东西!

THE SOLUTION to my little problem is 我的小问题的解决方案

/<a\\s*OnClick=\\"(.*?)\\"href=#>(.*?)<\\/a><td[^>]+>(.*?)<td[^>]+>(.*?)<\\/td><\\/tr>/m

thanks for the time! 感谢您的时间!

Luca Filosofi! 卢卡·菲洛索菲(Luca Filosofi)!

Wildly guessing here without actual sample data to match the regex against - also quite unhappy with having to use a regex here. 在这里没有实际示例数据来匹配正则表达式的情况下,人们会大肆猜测-对在这里必须使用正则表达式也很不满意。 Unless your tables always look exactly alike, I doubt you'll have much fun with regexes. 除非您的表始终看起来完全一样,否则我怀疑您会不会很喜欢正则表达式。

Anyway, all the caveats aside, this might work: 无论如何,撇开所有注意事项,这可能会起作用:

<tr[^>]+><th[^>]+><a OnClick="([^"]+)"\s*href="([^"]+)">([^<]+)</a><td[^>]+>([^<]+)<td[^>]+>([^<]+)</td></tr>

It expects the tags (and the attributes within the <a> tag) exactly in this order, no angle brackets within quoted strings, no escaped quotes within quoted strings etc. etc. (all those things that you wouldn't have to worry about if you used a parser). 它期望标签(和<a>标签内的属性)完全按此顺序排列,在带引号的字符串中没有尖括号,在带引号的字符串中没有转义引号,等等。(所有这些您不必担心的事情)如果您使用了解析器)。

In PHP: 在PHP中:

preg_match_all('%<tr[^>]+><th[^>]+><a OnClick="([^"]+)"\s*href="([^"]+)">([^<]+)</a><td[^>]+>([^<]+)<td[^>]+>([^<]+)</td></tr>%', $subject, $result, PREG_PATTERN_ORDER);

$result then is an array where $result[0] contains the entire match, $result[1] contains capturing group no. $result然后是一个数组,其中$result[0]包含整个匹配项, $result[1]包含捕获组号。 1, etc. 1等

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

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