I am trying to replace a space and forward slash in a url with a hyphen e.g www.example.com/car/bmw/porsche where bmw/porsche is one value..but it doesn't seem to be working for the forward slash.
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(" ", "/");
Which gave me the following: Notice: Array to string conversion
Then I tried getting them by their indexes like this:
$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
array (size=4)
0 => string '' (length=0)
1 => string 'car' (length=3)
2 => string 'bmw-porsche' (length=11)
3 => string '' (length=0)