从所有 中获取文本 字符串中的标签

由于我对正则表达式完全没用,并且在过去的半个小时里一直困扰着我,我想我会将其发布在这里,因为它可能非常简单。

<a href="/folder/files/hey/">hey.exe</a>
<a href="/folder/files/hey2/">hey2.dll</a>
<a href="/folder/files/pomp/">pomp.jpg</a>

在 PHP 中,我需要提取 标记之间的内容示例:

hey.exe
hey2.dll
pomp.jpg

Since I am completely useless at regex and this has been bugging me for the past half an hour, I think I'll post this up here as it's probably quite simple.

<a href="/folder/files/hey/">hey.exe</a>
<a href="/folder/files/hey2/">hey2.dll</a>
<a href="/folder/files/pomp/">pomp.jpg</a>

In PHP I need to extract what's between the <a> tags example:

hey.exe
hey2.dll
pomp.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗喜 2024-07-20 20:14:35

避免使用“.*”,即使您让它变得不贪婪,直到您对正则表达式进行了更多练习。 我认为对您来说一个好的解决方案是:

'/<a[^>]+>([^<]+)<\/a>/i'

注意“/”分隔符 - 您必须在 PHP 中使用正则表达式函数的 preg 套件。 它看起来像这样:

preg_match_all($pattern, $string, $matches);
// matches get stored in '$matches' variable as an array
// matches in between the <a></a> tags will be in $matches[1]
print_r($matches);

Avoid using '.*' even if you make it ungreedy, until you have some more practice with RegEx. I think a good solution for you would be:

'/<a[^>]+>([^<]+)<\/a>/i'

Note the '/' delimiters - you must use the preg suite of regex functions in PHP. It would look like this:

preg_match_all($pattern, $string, $matches);
// matches get stored in '$matches' variable as an array
// matches in between the <a></a> tags will be in $matches[1]
print_r($matches);
む无字情书 2024-07-20 20:14:35

这似乎有效:

$pattern = '/<a.*?>(.*?)<\/a>/';

This appears to work:

$pattern = '/<a.*?>(.*?)<\/a>/';
眼眸里的快感 2024-07-20 20:14:35

<a href="[^"]*">([^<]*)</a>

冰雪之触 2024-07-20 20:14:35

这是一个非常简单的:

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

但是,如果同一行中有多个匹配项,则应该小心,例如

<a href="/folder/hey">hey.exe</a><a href="/folder/hey2/">hey2.dll</a>

在这种情况下,正确的正则表达式是:

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

注意“?” 在“*”量词之后。 默认情况下,量词是贪婪的,这意味着它们会吃掉尽可能多的字符(意味着在本例中它们只会返回“hey2.dll”)。 通过附加引号,您可以使它们变得不贪婪,这应该更适合您的需求。

Here is a very simple one:

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

However, you should be careful if you have several matches in the same line, e.g.

<a href="/folder/hey">hey.exe</a><a href="/folder/hey2/">hey2.dll</a>

In this case, the correct regex would be:

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

Note the '?' after the '*' quantifier. By default, quantifiers are greedy, which means they eat as much characters as they can (meaning they would return only "hey2.dll" in this example). By appending a quotation mark, you make them ungreedy, which should better fit your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文