[英]Replace space and forward slash with hyphen in URL php
I am trying to replace a space and forward slash in a url with a hyphen eg www.example.com/car/ bmw/porsche where bmw/porsche is one value..but it doesn't seem to be working for the forward slash. 我想用连字符例如www.example.com/car/ 宝马/保时捷替换URL的空间和斜杠,其中宝马/保时捷是一个value..but似乎工作正斜杠不。
This is what I have for the space which works: 这就是我所拥有的空间:
$name = str_replace("-", " ", $request[2]);
$id = $category->find_id_by_name($name);
But when I add it for the forward slash, it doesn't work.. although it still works for the space: 但是当我为正斜杠添加它时,它不起作用..虽然它仍适用于空间:
$name = str_replace(array("-","//"), " ", $request[2]);
$id = $category->find_id_by_name($name);
How would I change this to work? 我该如何改变它?
EDIT 编辑
So I changed the code to the following: 所以我将代码更改为以下内容:
$char = " ";
$name = str_replace("-", $char, $request[2]);
$id = $category->find_id_by_name($name);
Then I put the $char
in an array like this $char = array(" ", "/");
然后我将
$char
放在一个像$char = array(" ", "/");
这样的$char = array(" ", "/");
Which gave me the following: Notice: Array to string conversion
Then I tried getting them by their indexes like this: 这给了我以下内容:
Notice: Array to string conversion
然后我尝试通过它们的索引获取它们,如下所示:
$name = str_replace("-", $char[0], $request[2]);<-- works for forward slash but not space
$name = str_replace("-", $char[1], $request[2]);<-- works for space but not forward slash
I just can't seem to get them to work together :( 我似乎无法让他们一起工作:(
[NEW EDIT] [新编辑]
class CarController extends Zend_Controller_Action {
public function indexAction() {
$category = new Application_Model_CarMapper();
$gcat = $this->getRequest()->getPathInfo();
$request = explode("/", $gcat);
//die(print_r($request));
if (isset($request[2])) {
$char = array("/"," ");
$name = str_replace("-", $char, $request[2]);//<-- "-" and "$char swapped over"
$id = $category->find_id_by_name($name);
$this->view->title = $id['name'];
//die(print_r($request));
$this->view->selectsub = $category->get_sub_cat_select($id['id']);
}
}
}
VAR DUMP VAR DUMP
array (size=4)
0 => string '' (length=0)
1 => string 'car' (length=3)
2 => string 'bmw-porsche' (length=11)
3 => string '' (length=0)
You could use urlencode()
for this: 您可以使用
urlencode()
:
$myUri = "Bmw/Porsche"; // Or set it accordingly by retreiving from your database
$uriCompatible = urlencode($myUri); // Results in "Bmw%2FPorsche"
$myUrl = "www.example.com/car/" . $uriCompatible;
You could use urldecode()
to retreive it: 您可以使用
urldecode()
来检索它:
echo $request[2]; // Results in "Bmw%2FPorsche"
$myUri = urldecode($request[2]); // Results in "Bmw/Porsche";
I am trying to replace a space and forward slash in a url with a hyphen
then you should have 那你应该有
$name = str_replace(" ", "-", $request[2]);
and 和
$name = str_replace(array(" ","/"), "-", $request[2]);
Use single quotes instead of double and it will work. 使用单引号而不是双引号它将起作用。 Like this:
像这样:
$name = str_replace(array('-','/'), " ", $request[2]);
$id = $category->find_id_by_name($name);
You do not have to escape the "/". 你不必逃避“/”。 backslashed need to be escaped here, but not foreward slashes.
需要在这里逃避反击,但不要向前冲去。
$name = str_replace(array("-","/"), " ", $request[2]);
$id = $category->find_id_by_name($name);
You don't need to escape forward slashes. 你不需要向前斜杠。 try this
尝试这个
$name = str_replace(array("-",'/'), " ", $request[2]));
尝试这个
$name = str_replace(array("-","/"), " ", $request[2]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.