0

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.

paulalexandru
  • 9,218
  • 7
  • 66
  • 94

4 Answers4

1

Is this OK?

$arr = array('books', 'art and design', '< 220g de CO2/Kg', '< 220g/ de CO2/Kg'); 
foreach($arr as $variable) {
    echo "$variable -> ";
    $variable = preg_replace('/\s+/', '_', $variable);
    $variable = preg_replace('~(?:^[^</]*[</]+|[</]+.*$)~', '', $variable);
    echo $variable,"\n";
}

Output:

books -> books
art and design -> art_and_design
< 220g de CO2/Kg -> _220g_de_CO2
< 220g/ de CO2/Kg -> _220g
Toto
  • 89,455
  • 62
  • 89
  • 125
  • I don't need everything. I need only the spaces to be replaced with _, and the rest of the wrong thing to be removed as in the example. – paulalexandru Apr 22 '15 at 13:29
  • I will test this and come back with an response. – paulalexandru Apr 22 '15 at 13:34
  • The output of your function is the same as the output I want to be ? Because as you can see I dont need just to remove the < and the / characters, but also what is after them. Check the output I've posted in the question. – paulalexandru Apr 22 '15 at 13:56
  • For $variable = '< 220g de CO2/Kg'; what is your output? – paulalexandru Apr 22 '15 at 14:01
  • The updated example is working just for those 3 examples I said, but it's not working in general. For example is you use "< 220g/ de CO2/Kg" it will output _220g/_de_CO2 and that is not right because it should output _200g . You can't have that / inside the url because the url will crash. See what I mean ? – paulalexandru Apr 23 '15 at 08:10
  • @paulalexandru: See my last edit. You should edit your question and add more test cases if this answer isn't correct. – Toto Apr 23 '15 at 08:41
-1

You could just use url_encode(): http://php.net/manual/en/function.urlencode.php and later url_decode() to get the real variable. Then you can work with it.

TheFrozenOne
  • 1,695
  • 9
  • 19
  • this will replace the `<` and `/` with their `%xxx` equivalents.. the op wants to replace with a custom slug – ddavison Apr 22 '15 at 13:24
  • 1
    @sircapsalot I know what he wants to do, but I guess OP just needs to encode them to later decode them. So he has the option to replace the `_` later with spaces or just use url_encode and _decode. Because he will need to replace them later. – TheFrozenOne Apr 22 '15 at 13:26
-1

In addition to url_encode() you may also use the following function whichh has a more "pretty" output:

function prettyURL($variable)
{
return  preg_replace('/^-+|-+$/', '', strtolower(preg_replace('/[^a-zA-Z0-9]+/', '_', $variable)));
}   

For your variables it produced the following output:

Variable 1: books
Variable 2: art_and_design
Variable 3: _220g_de_co2_kg

Update, removed strtolower:

function prettyURL($variable)
{
    return  preg_replace('/^-+|-+$/', '', preg_replace('/[^a-zA-Z0-9]+/', '_', $variable));
} 

Output:

Variable 1: books
Variable 2: art_and_design
Variable 3: _220g_de_CO2_Kg

Making strings "URL safe"

Community
  • 1
  • 1
Muggles
  • 1,526
  • 3
  • 14
  • 21
  • I dont need pretty output, i need exactly what I asked. – paulalexandru Apr 22 '15 at 13:26
  • @paulalexandru has the desired outcome to what you suggested, have you tried the code yet? – Muggles Apr 22 '15 at 13:30
  • 1
    @paulalexandru try the updated version, you just needed to remove strtolower. – Muggles Apr 22 '15 at 13:35
  • @paulalexandru apologies, I skimmed over the question. So to be clear, anything proceeding the forward slash character including the forward slash character? Or anything proceeding to the next two characters? Or anything proceeding until the next space? Could you post some more example variables and the desired output. – Muggles Apr 22 '15 at 14:01
-2

You can remove what you want using array (first parameter):

$whatRemove = array(' ', '<', 'Kg'); // add more if you need.
str_replace($whatRemove,'', $variable);