我正在尝试将url中的所有值转换为小写和连字符。但是当它转换为小写和连字符时,它不会收集记录,否则它会收集%20。我想您的反馈,看看编码是否正确,或者我还有什么需要做的。我已经检查了php和数组,它们对我来说似乎很好。
example.com/user/kate/baked/cake
>转到说明页面
example.com/user/kate/baked/Lovely%20Chocolate%20Cake
>转到说明页面
example.com/user/kate/baked/lovely-chocolate-cake
>这不会转到描述页面(这是我使用代码转换为小写和连字符的时候)
以下代码
htaccess的
RewriteRule ^user/.*[A-Z\ ] linkchange.php?l=%{REQUEST_URI} [L]
RewriteRule ^user/([a-z]+)/baked/([a-z.*0-9_-]+)$ cakes.php?id=$1&baked=$2
链接更改php
<?
$link = $_GET['l'];
$newlink = strtr($link," ABCDEFGHIJKLMNOPQRSTUVWXYZ","-abcdefghijklmnopqrstuvwxyz");
header("Location: http://www.example.com". $newlink,TRUE,301); exit;
?>
** cake php ** `
<?php
$cakesmade = array();
$cakesmade = array(
"kate" => array(
"cake" => array(
"cakeingredients" => "egg, flour"),
"Lovely Chocolate Cake" => array(
"cakeingredients" => "chocolate, eggs, flour"),
"amazing cake" => array(
"cakeingredients" => "lemons, flour")
),
);
?>
<?php foreach($cakesmade as $id => $donecake)
foreach($donecake as $bakedcake => $description)
{ echo "<a href='user/$id/baked/{$bakedcake}'>{$bakedcake}</a><br>";
}
?>
// this part should appear once the user and variable are set in the url
The selected cake is: <?php if(isset($bakedcake)) { echo $bakedcake; } else { echo "not set"; } ?> by <?php if(isset($id)) { echo $id; } else { echo "not set"; } ?>`
感谢 croises 到目前为止试图帮助我解决问题。
答案 0 :(得分:0)
假设您有这样的链接:
$str = example.com/user/kate/baked/Lovely%20Chocolate%20Cake
// we'll need to decode the URL like so :
$url = urldecode($str);
// then, replace lowercase and replace all spaces with dashes
$url = strtolower($url);
$url = str_replace(' ', '-', $url);
我想应该这样做。
答案 1 :(得分:0)
首先,从重写规则中删除第一个'/'。接下来,通过更改第一个规则使PHP的工作更容易。
RewriteRule ^user/([^/]+)/baked/(.*[A-Z\ ].*)$ linkchange.php?user=$1&cake=$2} [L]
RewriteRule ^user/([a-z]+)/baked/([a-z.*0-9_-]+)$ cakes.php?id=$1&baked=$2
您的链接更改代码将变为:
<?php
$user = $_GET['user'];
$cake = $_GET['cake'];
$cake = urldecode($cake);
$cake = strtr($cake," ABCDEFGHIJKLMNOPQRSTUVWXYZ","-abcdefghijklmnopqrstuvwxyz");
header("Location: http://www.example.com/user/$user/baked/$cake", TRUE, 301);
exit;
?>