I have a PHP function to remove characters in the url that the function FILTER_SANITIZE_URL
does not manage to clean but to achieve it I have to remove a line of code because a str_replace is already placed and what I want to know is if I can place more than str_replace and how I could do it.
Function to clean my url
// Limpiar url
function clearUrl($title){
// Pasar la variable titulo por la URL
$titleURL = str_replace(' ', '-', $title);
$titleURL = str_replace('?', '', $titleURL);
$titleURL = strtolower($titleURL);
$titleURL = filter_var($titleURL, FILTER_SANITIZE_URL);
return $titleURL;
}
What happens is that when I put this line of code $titleURL = str_replace('?', '', $title);
inside the function it ignores the first one and I need both because I want to eliminate this "?" sign of my url but I am looking for a way that with the same str_replace() I can replace spaces with "-" hyphens and "?" closing question marks for "" nothing
I hope you have understood what I want to achieve and give me a way to achieve it, greetings.
You have a problem in your function and it is that in the line of code where you try to replace "?" You are not placing the variable for nothing
$title
when you should place the variable$titleURL
so that the code can work well for you. Because currently, as your code is, what it does is that it ignores the line of code above when it is time to read the one below, causing the changes to replace the spaces with "-" hyphens to be lost.Code with the function already solved