php REGEX:抓取所有内容直到特定单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/147052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:40:32  来源:igfitidea点击:

REGEX: Grabbing everything until a specific word

phphtmlregex

提问by chicken

ex: <a><strike>example data in here</strike></a>

前任: <a><strike>example data in here</strike></a>

I want everything inside the a tag, to the end

我想要 a 标签中的所有内容,直到最后

/<a>([^<]*)<\/a>/

It works when there are no additional tags within the <a>tag, but what if there are?

当标签中没有其他标签时它会起作用<a>,但如果有呢?

I want to know if you can tell it to grab everything up to [^</a>]instead of [^<]only.

我想知道您是否可以告诉它抓取所有内容[^</a>]而不是[^<]仅抓取所有内容。

Doing it with /<a>(.*)<\/a>/doesn't work well. Sometimes I get everything in the <a>tag and other times I get tons of lines included in that call.

/<a>(.*)<\/a>/它做不好。有时我会得到<a>标签中的所有内容,而有时我会在该调用中包含大量行。

回答by Kibbee

/<a>(.*?)<\/a>/

should work. The ? makes it lazy, so it grabs as little as possible before matching the </a>part. but using . will mean that it matches everything until it finds </a>. If you want to be able to match across lines, you can use the following if with preg_match

应该管用。这 ?使它变得懒惰,因此它在匹配</a>零件之前尽可能少地抓取。但使用 . 将意味着它匹配所有内容,直到找到</a>. 如果您希望能够跨行匹配,则可以使用以下 if with preg_match

/<a>(.*?)<\/a>/s

The "s" at the end puts the regular expression in "single line" mode, which means the . character matches all characters including new lines. See other useful modifiers

末尾的“s”将正则表达式置于“单行”模式,这意味着 . 字符匹配包括新行在内的所有字符。查看其他有用的修饰符