简体   繁体   中英

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.

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)

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";

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM