php problem with backslash

1

I have a problem with the special character \ (backslash) in php. If I have the following code:

$f="aboca.png";
$ficheros[]=array("title" => $f, "description" => $f, "image" => "select\".$f");
pirnt_r($ficheros);

The result is:

Array ( [0] => Array ( [title] => aboca.png [description] => aboca.png [image] => select".aboca.png )

and I need the result to be as follows:

Array ( [0] => Array ( [title] => aboca.png [description] => aboca.png [image] => select\aboca.png )

I already know that \ is a special character and I've tried several combinations but there's no way.

    
asked by Jaroso Jaroso 21.04.2016 в 21:02
source

5 answers

1

This should work (\\):

$f="aboca.png";
$ficheros[]=array("title" => $f, "description" => $f, "image" => "select\".$f");
pirnt_r($ficheros);

There is also the PHP constant DIRECTORY_SEPARATOR .

Greetings

    
answered by 21.04.2016 в 21:26
1
$f = "aboca.png";
$ficheros = array("title" => $f, "description" => $f, "image" => "select" . "\" . $f );
print_r($ficheros);

You can check it in link

    
answered by 22.04.2016 в 15:35
0

I think it's because you have left over quotes at the end:

 "select\".$f");

after the $ f

    
answered by 21.04.2016 в 21:55
0

In PHP you can define the literals in different ways if you include it with double comma " PHP takes into account the special characters \ n \ t \ etc etc ...

Better to use ', your code with single quotes:

$f='aboca.png';
$ficheros[]=array('title' => $f, 'description' => $f, 'image' => 'select\'.$f);
pirnt_r($ficheros);

If you define literals using double comma, you should use \

$f="aboca.png";
$ficheros[]=array("title" => $f, "description" => $f, "image" => "select\$f");
pirnt_r($ficheros);
    
answered by 22.04.2016 в 11:15
0

You have several ways to solve it, taking into account that the main idea is to escape the character.

  • Escaping the '\' backSlash
    If a string is delimited with double quotes ("), PHP will interpret more escape sequences as special characters: more info

  • solution : '\\' backslash

    $ficheros = array("title" => $f, "description" => $f, "image" => "select\".$f);
    


    It can also look like this:

    $ficheros = array("title" => $f, "description" => $f, "image" => "select\.$f");
    

    Note: If you use double quotes (") you can put the variable php in without any problem.

        
    answered by 29.04.2016 в 13:18