littlea5h Posted May 1, 2013 Share Posted May 1, 2013 So i am trying to replace spaces and forward slashes in a url with a hyphen. Say we have a website named: www.example.com/car/bmw/porsche where bmw/porsche is one value I can seem to get it to work for a space but having trouble with the forward slash. This is what i have for the space (yes i know the search and replace are at opposite ends but it seems to work and doesn't work if i swap them round) $name = str_replace("-", " ", $request[2]); $id = $category->find_id_by_name($name); I tried a number of ways to make it work but it still doesn't seem to want to listen.. these are the ways i have tried but neither work: 1. $name = str_replace("-", array(" ", "/"), $request[2]); $id = $category->find_id_by_name($name); 2. $chars = array(" ", "/"); $name = str_replace("-", $chars, $request[2]); $id = $category->find_id_by_name($name); Both which produce the error:- Notice: Array to string conversion 3. $name = str_replace(array("-","//"), " ", $request[2]); $id = $category->find_id_by_name($name); In the above code, space works but not the forward slash. Tried changing the forward slash to a single one, single quotes with both one and two slashes but still nothing. I have spent a good couple of hours fiddling around with it but nothing seems to work Quote Link to comment https://forums.phpfreaks.com/topic/277489-str_replace-function-not-working-correctly/ Share on other sites More sharing options...
Barand Posted May 1, 2013 Share Posted May 1, 2013 <?php $str = "abc/def/gh i/x y"; $new = str_replace(array(' ','/'), '-', $str); echo "<pre>$str\n$new</pre>"; /**** result ******** abc/def/gh i/x y abc-def-gh-i-x-y *********************/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/277489-str_replace-function-not-working-correctly/#findComment-1427483 Share on other sites More sharing options...
littlea5h Posted May 1, 2013 Author Share Posted May 1, 2013 (edited) <?php $str = "abc/def/gh i/x y"; $new = str_replace(array(' ','/'), '-', $str); echo "<pre>$str\n$new</pre>"; /**** result ******** abc/def/gh i/x y abc-def-gh-i-x-y *********************/ ?> Thanks a bunch! I tested it for the '/' and it worked like a charm but i needed it for the space aswell, so after fiddling around,i put it inside an array again and I changed it to this: $char = array("/"," "); $name = str_replace("-", $char, $request[2]); $id = $category->find_id_by_name($name); However it doesnt work either, displays that error i stated above so i used the index to get the forward slash and it worked: e.g $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!! And from your example you have shown me, $request[2] is not shown but this is needed as this is where the data is, it would really help me out much if my code above was used Edited May 1, 2013 by littlea5h Quote Link to comment https://forums.phpfreaks.com/topic/277489-str_replace-function-not-working-correctly/#findComment-1427489 Share on other sites More sharing options...
Barand Posted May 1, 2013 Share Posted May 1, 2013 (edited) Change $char = array("/"," "); $name = str_replace("-", $char, $request[2]) to $char = array("/"," "); $name = str_replace($char, "-", $request[2]); edit : PS what does this give? print_r($request) Edited May 1, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/277489-str_replace-function-not-working-correctly/#findComment-1427492 Share on other sites More sharing options...
littlea5h Posted May 1, 2013 Author Share Posted May 1, 2013 Change $char = array("/"," "); $name = str_replace("-", $char, $request[2]) to $char = array("/"," "); $name = str_replace($char, "-", $request[2]); Does not work as search and replace works backwards for some reason :s Quote Link to comment https://forums.phpfreaks.com/topic/277489-str_replace-function-not-working-correctly/#findComment-1427493 Share on other sites More sharing options...
Barand Posted May 1, 2013 Share Posted May 1, 2013 and print_r($request) ? Quote Link to comment https://forums.phpfreaks.com/topic/277489-str_replace-function-not-working-correctly/#findComment-1427497 Share on other sites More sharing options...
littlea5h Posted May 1, 2013 Author Share Posted May 1, 2013 $char = array("/"," "); $name = str_replace($char, "-", $request[2]); edit : PS what does this give? print_r($request) With this code... Computer Systems in URL: Array ( [0] => [1] => systems[2] => computer-systems [3] => ) 1 Commun/networking in URL: Array ( [0] => [1] =>systems [2] => commun-networking [3] => ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/277489-str_replace-function-not-working-correctly/#findComment-1427524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.