php preg_替换这个网址

发布于 01-07 01:45 字数 424 浏览 2 评论 0原文

我想在数组插入 mysql 表之前替换这个 url。 我有标题描述和数组中的链接,并且链接有太多“垃圾”

这是 var_dump($item["link"]); 的示例看起来像: [“链接”]=> string(88) "http://www.hello.com/junk/junk-ju-junkj-junk-jun-ju-junk,-JU-1234"

我想像这样替换链接: [“链接”]=> string(88) "http://www.hello.com/te/tst?te=1234"

因此搜索 "http://www.hello.com" 和 "very last '-' ...before 1234 之间的所有内容” 并将其替换为“/te/tst?te=”

在插入 mysql 表之前如何替换它?

预先感谢您的专业知识和时间;)

I want to replace this url, before array gets inserted in mysql table.
I have title description and link in array, and link has too much "junk"

Here is sample of what var_dump($item["link"]); looks like:
["link"]=> string(88) "http://www.hello.com/junk/junk-ju-junkj-junk-jun-ju-junk,-JU-1234"

I would like to get link replaced like this:
["link"]=> string(88) "http://www.hello.com/te/tst?te=1234"

So search everything between "http://www.hello.com" and "very last '-' ...before 1234"
and replace it with "/te/tst?te="

How do I replace this before it gets inserted in mysql table?

Thank you in advance for your expertise and time ;)

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

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

发布评论

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

评论(1

软的没边2025-01-14 01:45:56

您预计除“com”之外还有其他顶级域名吗?假设

$item['link'] = 'http://www.hello.com/junk/junk-ju-junkj-junk-jun-ju-junk,-JU-1234';

你能做到

$pattern = '/(^http:\/\/\w+\.\w+\.com).*?-(\d+)/';
$replacement = '$1/te/tst?te=$2';
$item['link'] = preg_replace($pattern, $replacement, $item['link']);

你会得到

$item['link'] = 'http://www.hello.com/te/tst?te=1234';

Do you anticipate other top-level domains other than 'com'? Assuming

$item['link'] = 'http://www.hello.com/junk/junk-ju-junkj-junk-jun-ju-junk,-JU-1234';

You can do

$pattern = '/(^http:\/\/\w+\.\w+\.com).*?-(\d+)/';
$replacement = '$1/te/tst?te=$2';
$item['link'] = preg_replace($pattern, $replacement, $item['link']);

You'll get

$item['link'] = 'http://www.hello.com/te/tst?te=1234';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文