我如何替换链接,另一个使用的preg_replace?

问题描述 投票:0回答:4

我试图像删除“删除/”从链接

   preg_replace("https://mywebsite.com/remove/","https://mywebsite.com/")
php
4个回答
4
投票

您可以使用str_replace()

str_replace($search, $replace, $subject)

  • $search是针
  • $replace是更换
  • $subject是我们要修改字符串

所以你的情况这将是:

str_replace('remove/', '', 'https://mywebsite.com/remove/');


1
投票

使用str_replace从您的字符串替换特定部分。

echo str_replace("remove/", "", "https://mywebsite.com/remove/");

1
投票

使用str_replace()

print(str_replace("/remove/", "", $link));

0
投票

虽然@共感官的回答绝对会工作 - 我想提出另一种解决方案。如果你的链接到https://mywebsite.com/remove/已收录,你可能需要使用一个301 Redirect到您的主页和/或发出删除页面上的410 "Gone"响应。

如果你将有很多被移动/缺少的资源,您可能需要使用插件像Simple 301 Redirects。否则,你可以简单地添加像.htaccess规则

Redirect 301 /remove https://mywebsite.com/

或通过PHP添加它,如果你更舒适的与

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: " . site_url() ); 
exit();
© www.soinside.com 2019 - 2024. All rights reserved.