So basically I have a php variable which I want to add in a url. The problem is that if the variable contains strange characters inside it, the url will not work.
My example:
URL: http://localhost/myapp/products/$variable
And the variable can look like this:
$variable = 'books'; //books
$variable = 'art and design'; //art_and_design
$variable = '< 220g de CO2/Kg'; //_220g_de_CO2
$variable = '< 220g /de CO2/Kg'; //_220g_
http://localhost/myapp/products/books
http://localhost/myapp/products/art_and_design
http://localhost/myapp/products/_220g_de_CO2
http://localhost/myapp/products/_220g_
I want to strip the variable to look like the values in the comments, meaning that the spaces should be replaced by _ and in the same time remove the parts that get in conflict with the url (< or /).
As you can see, I've removed the first char and the last 3, and kept only the good part because I use that for a search in the database (LIKE opperator) and it wouldn't work if I removed only the /
and kept Kg
also.
I need a function to do this for me, currently it looks like this:
function stripVar($variable){
return str_replace(' ', '_', $variable);
}
But I don't know how to handle the other part. Thanks.