Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace space and forward slash with hyphen in URL php
    text
    copied!<p>I am trying to replace a space and forward slash in a url with a hyphen e.g www.example.com/car/<strong>bmw/porsche</strong> where bmw/porsche is one value..but it doesn't seem to be working for the forward slash.</p> <p>This is what I have for the space which works:</p> <pre><code>$name = str_replace("-", " ", $request[2]); $id = $category-&gt;find_id_by_name($name); </code></pre> <p>But when I add it for the forward slash, it doesn't work.. although it still works for the space:</p> <pre><code>$name = str_replace(array("-","//"), " ", $request[2]); $id = $category-&gt;find_id_by_name($name); </code></pre> <p>How would I change this to work?</p> <p><strong>EDIT</strong></p> <p>So I changed the code to the following:</p> <pre><code>$char = " "; $name = str_replace("-", $char, $request[2]); $id = $category-&gt;find_id_by_name($name); </code></pre> <p>Then I put the <code>$char</code> in an array like this <code>$char = array(" ", "/");</code> Which gave me the following: <code>Notice: Array to string conversion</code> Then I tried getting them by their indexes like this:</p> <pre><code>$name = str_replace("-", $char[0], $request[2]);&lt;-- works for forward slash but not space $name = str_replace("-", $char[1], $request[2]);&lt;-- works for space but not forward slash </code></pre> <p>I just can't seem to get them to work together :(</p> <hr> <p><strong>[NEW EDIT]</strong></p> <pre><code>class CarController extends Zend_Controller_Action { public function indexAction() { $category = new Application_Model_CarMapper(); $gcat = $this-&gt;getRequest()-&gt;getPathInfo(); $request = explode("/", $gcat); //die(print_r($request)); if (isset($request[2])) { $char = array("/"," "); $name = str_replace("-", $char, $request[2]);//&lt;-- "-" and "$char swapped over" $id = $category-&gt;find_id_by_name($name); $this-&gt;view-&gt;title = $id['name']; //die(print_r($request)); $this-&gt;view-&gt;selectsub = $category-&gt;get_sub_cat_select($id['id']); } } } </code></pre> <p><strong>VAR DUMP</strong></p> <pre><code>array (size=4) 0 =&gt; string '' (length=0) 1 =&gt; string 'car' (length=3) 2 =&gt; string 'bmw-porsche' (length=11) 3 =&gt; string '' (length=0) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload