Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP更改url_Php_Regex - Fatal编程技术网

使用PHP更改url

使用PHP更改url,php,regex,Php,Regex,我需要创建多语言链接到我的网站,我想使用str_替换,但我不确定这是否是正确的选择。也许正则表达式会起作用。。。但是我的正则表达式不是很流利 我的URL如下所示: localhost/project/public/LANG/event/id/ 语言可以是: /en/ /fi/ /sv/ 否则url是相同的,只是url的/LANG/部分发生了更改。 我已尝试使用以下内容更改url: <?php $search = array("/sv/", "/fi/"); $replace = arr

我需要创建多语言链接到我的网站,我想使用str_替换,但我不确定这是否是正确的选择。也许正则表达式会起作用。。。但是我的正则表达式不是很流利

我的URL如下所示: localhost/project/public/LANG/event/id/

语言可以是:

/en/ 
/fi/
/sv/
否则url是相同的,只是url的/LANG/部分发生了更改。 我已尝试使用以下内容更改url:

<?php
$search = array("/sv/", "/fi/");
$replace = array("/en/");
$url_en = str_replace($search, $replace, $url);
echo $url_en;
?>


然而,这并没有给我正确的url,它用sv或fi替换了所有内容。我认为正确的解决方案是使用.htaccess文件翻译frindly URL

preg_replace('|(localhost/project/public/)..|', '\1EN', $url);
在根网站目录中创建.htaccess文件

你的链接看起来像域/a/b/c/d

localhost/project/public/LANG/event/id/

这被翻译成domain/index.php?action=project&saction=public&language=LANG&traction=event&tid=id

它们被这个文件翻译成domain/index.php?action=a&id=b&saction=c&sid=d

下面是我的一个文件,例如:;祝你好运

Options +SymLinksIfOwnerMatch

RewriteEngine on


RewriteRule ^([a-zA-Z-]+)/(producator)-([0-9]+)$ index.php?action=$1&producator=$3 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(producator)-([0-9]+)$ index.php?action=$1&id=$2&producator=$4 [NC,L]

RewriteRule ^([a-zA-Z-]+)/(producatorx)-([0-9]+)$ index.php?action=$1&producatorx=$3 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(producatorx)-([0-9]+)$ index.php?action=$1&id=$2&producatorx=$4 [NC,L]

RewriteRule ^([a-zA-Z-]+)$ index.php?action=$1 [NC,L]

RewriteRule ^([a-zA-Z-]+)-([0-9]+)$ index.php?action=$1&id=$2 [NC,L]

RewriteRule ^(tag)/([a-z0-9]+)$ index.php?action=tag&tag=$2 [NC,L]

RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(update)-([a-z0-9]+)$ index.php?action=$1&id=$2&saction=update&code=$4 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(delete)-([a-z0-9]+)$ index.php?action=$1&id=$2&saction=delete&code=$4 [NC,L]

RewriteRule ^([a-zA-Z-]+)-([0-9]+)/([a-zA-Z-]+)-([0-9]+)$ index.php?action=$1&id=$2&saction=$3&sid=$4 [NC,L]

RewriteRule ^(.*)-([0-9]+).html$ index.php?action=details&id=$2 [NC,L]

@布万·里卡,正如你在下面看到的,这不是真的;-)这是使用.htaccess文件完成的。使用htaccess文件并建立所有URL结构;这是非常有用的,因为如果url结构匹配,您知道所有变量和值,但由于您的rep较低,因此不需要-1,但是您是否只是在某个地方转储了正在使用的.htaccess文件之一?我看不出与所问的问题和你的回答有什么关系。问题是关于更改URL以包括EN、FI、SV等,但您所说的是producator、actions。要么修改你的答案来回答这个问题,否则人们会投反对票1.他必须替换一些词,他必须抓住要点;他的链接看起来如何,必须如何翻译;我认为这是解决他与Links的所有问题的正确方法,但即使是将13+行纯htaccess倾注在某人身上,每行都不做任何解释,就像说
嘿,你!!,看这里,挑你想要的然后走开!祝你好运。
不管怎样,这只是我的意见,你当然可以自由地以任何你想要的方式回答。不,对不起,我仍然很难在.htaccess中处理ReGex