从 URL 路径字符串中删除语言说明符
我正在寻找可以帮助我处理以下内容的正则表达式的人。
我有这个代码:(已更新)
<?php
$sitename = "http://" .$_SERVER["SERVER_NAME"];
$sitename = mysql_real_escape_string($sitename);
$language = "da";
$language = mysql_real_escape_string($language);
$pagename = $_SERVER["PHP_SELF"];
$pagename = mysql_real_escape_string($pagename);
$language1 = preg_replace("/$language/", "$1", "$pagename");
?>
我需要一个正则表达式来从 URL(站点名称)中删除语言 - 现在可以使用
如何转义特殊字符?
上面示例的结果留给我的是 //index.asp
而不是 /index.asp
。
基本上,我想要做的是从 URL 中删除常量 (/da
):
URL 将如下所示 http://www.example.com/da/ 或
http://www.example.com/da/folder/folder/folder/page.asp
我只需要从 URL 中取出 da
即可。
我将如何在 PHP 中执行此操作?
好吧,我似乎已经弄清楚了:
<?php
$sitename = "http://" .$_SERVER["SERVER_NAME"];
$sitename = mysql_real_escape_string($sitename);
$language = "\/da";
$pagename = $_SERVER["PHP_SELF"];
$pagename = mysql_real_escape_string($pagename);
$language1 = preg_replace("/$language/", "$1", "$pagename");
?>
我只需要删除这一行:
$language = mysql_real_escape_string($language);
I am looking for someone that can help me with a regex for the following.
I have this code: (have updated it)
<?php
$sitename = "http://" .$_SERVER["SERVER_NAME"];
$sitename = mysql_real_escape_string($sitename);
$language = "da";
$language = mysql_real_escape_string($language);
$pagename = $_SERVER["PHP_SELF"];
$pagename = mysql_real_escape_string($pagename);
$language1 = preg_replace("/$language/", "$1", "$pagename");
?>
I need a regex to strip the language from the URL(sitename) - This now works
How do I escape special characters?
The result from the above example leaves me with //index.asp
and not with /index.asp
.
Basically, what I want to do is to strip a constant (/da
) from a URL:
the URL will look like this http://www.example.com/da/
or http://www.example.com/da/folder/folder/folder/page.asp
I only need to take the da
out of the URL.
How will I do this in PHP?
Ok I seemed to have figured it out:
<?php
$sitename = "http://" .$_SERVER["SERVER_NAME"];
$sitename = mysql_real_escape_string($sitename);
$language = "\/da";
$pagename = $_SERVER["PHP_SELF"];
$pagename = mysql_real_escape_string($pagename);
$language1 = preg_replace("/$language/", "$1", "$pagename");
?>
I only needed to remove this line:
$language = mysql_real_escape_string($language);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
替换:
搜索:
Replace:
Search:
您应该发布一些要解析的真实字符串(这个问题对于您发布的代码没有什么意义,因为您已经在
$language
变量中拥有数据)。无论如何,在 PHP 中,您可以使用 parse_url() 将 URL 拆分为其组件,然后:确切的细节取决于您的数据是什么样子。
顺便说一句,您的 JavaScript 代码会将
/data/en
更改为/sitenameta/en
:-?You should post some real strings you want to parse (the question makes little sense with the code you post since you already have the data in the
$language
variable). Whatever, in PHP you can use parse_url() to split the URL into its components and then:The exact details depend on what your data looks like.
BTW, your JavaScript code will change
/data/en
into/sitenameta/en
:-?