我想用連字符替換空格和正斜槓,例如www.example.com/car/bmw/porsche其中bmw/porsche是一個值..但它似乎並沒有爲正斜槓工作。在URL中用連字符替換空格和正斜線php
這是我有其運作空間:
$name = str_replace("-", " ", $request[2]);
$id = $category->find_id_by_name($name);
但是當我添加它正斜槓,這是行不通的。雖然它仍然適用於空間:
$name = str_replace(array("-","//"), " ", $request[2]);
$id = $category->find_id_by_name($name);
我該如何改變這個工作?
編輯
所以我改變了代碼如下:
$char = " ";
$name = str_replace("-", $char, $request[2]);
$id = $category->find_id_by_name($name);
然後我把$char
陣列中的這樣$char = array(" ", "/");
這給了我如下: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
我只是不能似乎讓他們一起工作:(
[NEW編輯]
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
array (size=4)
0 => string '' (length=0)
1 => string 'car' (length=3)
2 => string 'bmw-porsche' (length=11)
3 => string '' (length=0)
你在哪裏定義$ request [2]變量?你可以發佈'var_dump($ request);'的結果嗎? – Frank 2013-05-01 16:32:07
請看我上面的編輯...我已經顯示了完整的代碼 – ash 2013-05-02 09:20:35
$ name有什麼值?我認爲在這個例子中,你最終希望它成爲寶馬保時捷。你究竟得到了什麼? – Belinda 2013-05-02 09:32:56