0

I'm new to programming, you could help me with my question below:

I have the line below where I replace the Blank Space "" for dash (-)

$url = addslashes( 'mysite.com/pages/'.strtolower(str_replace(' ','-', $q)) ).'.html';

but how do I put it on the same line as the code to also replace the question mark (?) And Ampersand (&) for empty ""

I tried the way below but it didn't work, I'm missing something

$url = addslashes( 'mysite.com/pages/'.strtolower(str_replace('?','',(str_replace('&','',(str_replace(' ','-', $q)))))) ).'.html';
Jean
  • 47
  • 6
  • 1
    Does this answer your question? [Str\_replace for multiple items](https://stackoverflow.com/questions/7605480/str-replace-for-multiple-items) – George Pant Apr 24 '20 at 02:49

2 Answers2

0

You can use this code to replace space with "-" .

'The text you want to change'.toLowerCase().split(" ").join("-");

or

var a = 'The text you want to change';
a = a.toLowerCase().split(" ").join("-");
Haha
  • 148
  • 1
  • 8
0

It will help you

$your_string = 'you_input_string';
$search = [" ", "?", "&"];
$replace = ["-", "", ""];
echo str_replace($search, $replace, $your_string);
Ravindra Patel
  • 307
  • 3
  • 14
  • I put it as below but it didn't work $url = addslashes( 'mysite.com/page/'.strtolower(str_replace(array('?', ''),(array('&', ''), array(' ','-', $q)))))) ).'.html'; – Jean Apr 24 '20 at 03:40
  • Can you tell me why you are using addslashes() function? And what's your input string and what kind of output you want? – Ravindra Patel Apr 25 '20 at 12:16
  • it is a search site and when it is searched for example (fast car) the search is saved on my sitemap as https://hotbusca.com/paginas/fast-car.html However, if the search contains symbols (& or?) The sitemap link does not work. link of my sitemap: https://hotbusca.com/sitemap.xml?id=2 – Jean Apr 25 '20 at 19:31
  • When you get search value from the input box, store the value in a variable then apply str_replace method and after that append it to your site URL – Ravindra Patel Apr 27 '20 at 03:41