URL重写 - 在斜杠之间更改URL文本

时间:2015-05-24 13:36:44

标签: php url rewrite

我有这样的网址:

http://www.mywebsite.com/carmake/ABCDEFG/123456789
http://www.mywebsite.com/carmake/AAABBBC/124532532
http://www.mywebsite.com/carmake/BNDFKNV/463634213

我希望将它们更改为:

http://www.mywebsite.com/carmake/parts/123456789
http://www.mywebsite.com/carmake/parts/124532532
http://www.mywebsite.com/carmake/parts/463634213

如何更改functions.php

中最后一个到斜杠之间的文本

3 个答案:

答案 0 :(得分:0)

https://regex101.com/r/sH1wA1/1

<?php

$string      = 'http://www.mywebsite.com/carmake/ABCDEFG/123456789';
$pattern     = '/(.*\/).*\/([^\/]*$)/';
$replacement = '${1}parts/${2}';

    echo preg_replace($pattern, $replacement, $string);
?>

答案 1 :(得分:0)

试试这个:

<?php
$url = "http://www.mywebsite.com/carmake/ABCDEFG/123456789";
$parts = parse_url($url);
$path = $parts['path'];
$pos = strpos($path, '/', 9);
$sub = substr($path, 9, $pos - 9);
$url = str_replace($sub, 'parts', $url);

答案 2 :(得分:0)

拆分为细分,更改并收回

$a = 'http://www.mywebsite.com/carmake/BNDFKNV/463634213';
$to = 'parts';
$s = explode('/', $a);
$s[count($s)-2] = $to;
echo implode('/', $s);