Jump to content

str_replace function not working correctly


littlea5h

Recommended Posts

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   :confused:

 

 

 

 

Link to comment
Share on other sites

<?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  :happy-04:

Edited by littlea5h
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

 $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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.