Reputation: 93
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)
Upvotes: 0
Views: 4822
Reputation: 12621
You could use urlencode()
for this:
$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:
echo $request[2]; // Results in "Bmw%2FPorsche"
$myUri = urldecode($request[2]); // Results in "Bmw/Porsche";
Upvotes: 1
Reputation: 4111
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]);
Upvotes: 1
Reputation: 11830
You don't need to escape forward slashes. try this
$name = str_replace(array("-",'/'), " ", $request[2]));
Upvotes: 0
Reputation: 2600
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);
Upvotes: 0
Reputation: 91
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);
Upvotes: 0