douhan4093
2013-05-01 09:30
浏览 64
已采纳

在URL php中用连字符替换空格和正斜杠

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)
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • douqun1977 2013-05-01 09:33
    已采纳

    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";
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxizhe9755 2013-05-01 09:33

    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);
    
    评论
    解决 无用
    打赏 举报
  • dongniechi7825 2013-05-01 09:34

    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);
    
    评论
    解决 无用
    打赏 举报
  • douweibiao8471 2013-05-01 09:35

    You don't need to escape forward slashes. try this

    $name = str_replace(array("-",'/'), " ", $request[2]));
    
    评论
    解决 无用
    打赏 举报
  • doudunyi3796 2013-05-01 09:40

    Try this

    $name = str_replace(array("-","/"), " ", $request[2]);
    
    评论
    解决 无用
    打赏 举报
  • douyi5157 2013-05-01 09:50

    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]);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题