需要有关 php 中的正则表达式替换的帮助

发布于 2024-12-01 12:50:05 字数 319 浏览 0 评论 0原文

我有一个包含此模式链接的字符串:

<a href="http://randomurl.com/random_string;url=http://anotherrandomurl.com/">xxxx</a>

我想删除“http://xxx.xxx.xxx/random_string;url=”并保留字符串的其余部分,留在最后

<a href="http://anotherrandomurl.com/">xxxx</a>

有人可以帮忙吗?

i have a string which includes links of this pattern:

<a href="http://randomurl.com/random_string;url=http://anotherrandomurl.com/">xxxx</a>

i want to remove "http://xxx.xxx.xxx/random_string;url=" and keep the rest of the string, leaving at the end

<a href="http://anotherrandomurl.com/">xxxx</a>

Can anyone help please ?

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

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

发布评论

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

评论(4

救星 2024-12-08 12:50:05

使用:

$new_link = preg_replace('/<a href="(?:.+);url=([^"]+)">/', '<a href="$1">', $url);

Use:

$new_link = preg_replace('/<a href="(?:.+);url=([^"]+)">/', '<a href="$1">', $url);
神魇的王 2024-12-08 12:50:05

有多种方法可以实现您想要的结果。 regex 的替代方法是使用 strpos 并删除这些字符以及前面的字符。

There are multiple methods for achieving your desired result. An alternative from regex would be to find the occurence of url= using strpos and remove those characters and the preceeding characters as well.

苯莒 2024-12-08 12:50:05

这比你想象的要棘手,我敦促你 避免使用正则表达式

相反,您应该使用 HTML 解析器查找文档中的所有 标记,然后在 ;url= 上拆分它们的 href 属性并只保留最后一部分。

但是,如果您必须使用正则表达式,则以下内容应该适用于大多数格式良好的 HTML:

preg_replace('/(<\s*a\s[^>]*href=)(["\'])(?:[^\1]*;url=)([^\1]*)(\1[^>]*>)/i', "$1$2$3$4", $url)

说明:

(<\s*a\s[^>]*\bhref=) # <a, optionally followed by other attributes, and then href. Whitespace is ignored. This will be captured in backreference $1.
(["\'])               # Either " or ' to enclose the href value. This will be captured in $2 for matching later.
(?:[^\1]*;url=)       # Any number of URLs followed by ";url=". This will be thrown out.
([^\1]*)              # This is the URL you want to keep. It will keep matching until the end of the quotes. This will be captured into $3.
(\1[^>]*>)            # The remainder of the <a> tag, including any other attributes. This is captured in $4.

This is trickier than you think, and I urge you to avoid using regex for it.

Instead, you should use an HTML parser to find all <a> tags in the document, then split their href attributes on ;url= and keep only the last part.

However, if you must use a regex, the following should work for most well-formed HTML:

preg_replace('/(<\s*a\s[^>]*href=)(["\'])(?:[^\1]*;url=)([^\1]*)(\1[^>]*>)/i', "$1$2$3$4", $url)

Explanation:

(<\s*a\s[^>]*\bhref=) # <a, optionally followed by other attributes, and then href. Whitespace is ignored. This will be captured in backreference $1.
(["\'])               # Either " or ' to enclose the href value. This will be captured in $2 for matching later.
(?:[^\1]*;url=)       # Any number of URLs followed by ";url=". This will be thrown out.
([^\1]*)              # This is the URL you want to keep. It will keep matching until the end of the quotes. This will be captured into $3.
(\1[^>]*>)            # The remainder of the <a> tag, including any other attributes. This is captured in $4.
ぃ弥猫深巷。 2024-12-08 12:50:05
$new_link = preg_replace('~(\shref=")[^"]+?(?<=;url=)~', '$1', $url);
$new_link = preg_replace('~(\shref=")[^"]+?(?<=;url=)~', '$1', $url);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文