简体   繁体   English

如何在 PHP 中将 www.mydomain.com/page/user/756890 转为 www.mydomain.com

[英]How to turn www.mydomain.com/page/user/756890 to www.mydomain.com in PHP

How could I replace the URL of a string from this:我怎么能从这个替换字符串的 URL:

798| 451.263.160.11| CONNECT | http://www.mywebsite.com/pages/user/678976546789 | China | 22 min

To this in PHP:在 PHP 中:

798| 451.263.160.11| CONNECT | http://www.mywebsite.com | China | 22 min

Thanks guys :)谢谢你们 :)

[NOTE: The text is from a file] [注意:文本来自文件]

I'm not sure what you mean by "register file", but it looks like you're trying to do a regex replacement .我不确定您所说的“注册文件”是什么意思,但看起来您正在尝试进行正则表达式替换

$input = '798| 451.263.160.11| CONNECT | http://www.mywebsite.com/pages/user/678976546789 | China | 22 min';

$search_for = 'http://www.mywebsite.com';

$output = preg_replace('/' . preg_quote($search_for, '/') . '([^ ]+)/', $search_for, $input);

The code above will search for the string you provide, and remove all characters until the next space.上面的代码将搜索您提供的字符串,并删除所有字符直到下一个空格。

You can find out more about regex in PHP using the manual:您可以使用手册找到有关 PHP 中正则表达式的更多信息:

http://php.net/preg_replace http://php.net/preg_replace

http://php.net/manual/en/reference.pcre.pattern.syntax.php http://php.net/manual/en/reference.pcre.pattern.syntax.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .htaccess www.mydomain.com/index.php?page=home到www.mydomain.com/home - .htaccess www.mydomain.com/index.php?page=home to www.mydomain.com/home 需要使用 www.mydomain.com/filename/ 而不是 www.mydomain.com/file.php?id=1 - need to use www.mydomain.com/filename/ instead of www.mydomain.com/file.php?id=1 从www.mydomain.com/foo重定向到www.mydomain.com/foo/ - Redirect from www.mydomain.com/foo to www.mydomain.com/foo/ Laravel 4.1使用301永久重定向将所有请求从www.mydomain.com重定向到mydomain.com - Laravel 4.1 Redirect all requests from www.mydomain.com to mydomain.com with 301 permanent redirect 只允许PHP脚本打开网址,如www.mydomain.com/topsecret/1/001.jpg - only allow PHP scripts to open urls like www.mydomain.com/topsecret/1/001.jpg 网址重写:将mydomain.com/index.php更改为mydomain.com/home - URL rewriting: Change mydomain.com/index.php to mydomain.com/home PHP邮件未从mydomain.com发送到Google应用程序电子邮件myname@mydomain.com - PHP mail is not sending from mydomain.com to the google apps email myname@mydomain.com 如何在Codeigniter中使用mobiledetect.net将mydomain.com重定向到m.mydomain.com? - How to redirect mydomain.com to m.mydomain.com with mobiledetect.net in codeigniter? 在Heroku上将app.mydomain.com作为mydomain.com/app运行 - Run app.mydomain.com as mydomain.com/app on Heroku 如何使用PHP curl来获取www.yahoo.com页面? - How to use PHP curl to fetch www.yahoo.com page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM