从 URL 路径字符串中删除语言说明符

发布于 2024-10-22 05:38:27 字数 1206 浏览 6 评论 0原文

我正在寻找可以帮助我处理以下内容的正则表达式的人。

我有这个代码:(已更新)

<?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 技术交流群。

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

发布评论

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

评论(2

浮生面具三千个 2024-10-29 05:38:27

替换:

$re = '/(?:\w+:\/\/[\w][\w.]+\/)(\w+)\//ui';
or this $re = '^(?:.*)\/(\w{2})\//ui';

$text = http://www.domain.com/ru/

preg_replace($re, 'ru', $text);

--> *http://www.domain.com/ru/*

搜索:

$re = '/(?<domain>\w+:\/\/[\w][\w.]+\/)(?<lang>\w+)\//ui';
or this $re = '^(?:.*)\/(?<lang>\w{2})\//ui';

$text = http://www.domain.com/ru/

preg_match($re, $text, $aMatches);

--> Array
(
    [0] => http://www.domain.com/da/
    [domain] => http://www.domain.com/
    [1] => http://www.domain.com/
    [lang] => da
    [2] => da
)

Replace:

$re = '/(?:\w+:\/\/[\w][\w.]+\/)(\w+)\//ui';
or this $re = '^(?:.*)\/(\w{2})\//ui';

$text = http://www.domain.com/ru/

preg_replace($re, 'ru', $text);

--> *http://www.domain.com/ru/*

Search:

$re = '/(?<domain>\w+:\/\/[\w][\w.]+\/)(?<lang>\w+)\//ui';
or this $re = '^(?:.*)\/(?<lang>\w{2})\//ui';

$text = http://www.domain.com/ru/

preg_match($re, $text, $aMatches);

--> Array
(
    [0] => http://www.domain.com/da/
    [domain] => http://www.domain.com/
    [1] => http://www.domain.com/
    [lang] => da
    [2] => da
)
爱要勇敢去追 2024-10-29 05:38:27

您应该发布一些要解析的真实字符串(这个问题对于您发布的代码没有什么意义,因为您已经在 $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 :-?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文