1

I read a string from config.ini the string is

config.ini
[section]
param = "#1234\y"

then when I read this code and print it in logs or try to send it to the server or socket this will print the following:

#1234\\y

How can I make sure I will send only one backslash

also when I get from config without \y . and try to concatenate that via php like $x.'\y' it is the same and not working.

it's working only if I define a variable with the string and not from the config

my udp server I need to send the string

    //Send the message to the server
    if( ! socket_sendto($sock, $newstr , strlen($newstr) , 0 , $ip , $port))
    {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
        throw new Exception("Could not send data: $errorcode $errormsg \n");
    }
Tuz
  • 1,810
  • 5
  • 29
  • 58
  • Hmm, just read the [manual](http://php.net/manual/en/language.types.string.php#language.types.string.parsing) about strings, it’s all there. – dbf Aug 25 '18 at 08:53

3 Answers3

1

the problem is wrongfully escaped input - not wrongfully escaped output.

either try parse_ini_file ($filename, true, INI_SCANNER_RAW); in order to disable parsing, which would lead to not to escaping the backslashes - or change the INI to param = '#1234\y'.

in case not able to fix the string input, here's how to fix the output string (the worst possible solution):

$newstr = str_replace('\\\\', '\\', $mystr);

or

$newstr = preg_replace('/\\\\/', '\\', $mystr);
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • i can't change the ini reading . is there any option besides that ? – Tuz Aug 25 '18 at 08:58
  • @Tuz `INI_SCANNER_RAW` might be the best option, because it disables the parsing, therefore also disables the escaping of backslashes; else you would have to manipulate the resulting array. the docs read: `$scanner_mode` Can either be `INI_SCANNER_NORMAL` (default) or `INI_SCANNER_RAW`. If `INI_SCANNER_RAW` is supplied, then option values will not be parsed.` – Martin Zeitler Aug 25 '18 at 09:39
  • @Tuz why you can't change the `INI` reading? you might probably read those values somewhere? – Martin Zeitler Aug 25 '18 at 09:42
  • Because there are also lot of in I parans and I can't make regression – Tuz Aug 25 '18 at 09:50
  • @Tuz while no single one of those parameters would need to be parsed (and escaped), this would be no regression at all, but rather be the expected behavior of `parse_ini_file()`. "can't change that code because I'm afraid that something could break" is not really an argument... while able to revert to the previous revision at any time. – Martin Zeitler Aug 25 '18 at 09:57
  • it is still print with // . instaed / (your preg_replace solution) – Tuz Aug 25 '18 at 10:14
  • @Tuz you might manipulate the wrong string, or assign it to the wrong variable. simply fix the function which reads the `INI` and the problem would be properly fixed, instead of adding some questionable workaround... it seems you ignore my answer, in particular the first line of it. – Martin Zeitler Aug 25 '18 at 10:16
  • done but it makes problem with others params like db connection string ..etc is there any other option ? – Tuz Aug 25 '18 at 10:27
  • also i talking about slash . and not backslash – Tuz Aug 25 '18 at 10:28
  • and i tried it in and it is not working . and sorry I meant slash and not backslash – Tuz Aug 25 '18 at 10:33
0

Since you are reading from a config file you could also using single quotes around the value definition.

param = ‘#1234\y’

This will ensure the backslash is not escaped. For information check out this great post.

If reading from reading from a script you could consume the var using the PHP method, ‘stripslashes’.

stripslashes($param)

From the PHP manual:

Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\) are made into a single backslash ().

atoms
  • 2,993
  • 2
  • 22
  • 43
  • why you copy my answer? besides those quotation marks are obviously a syntax error. – Martin Zeitler Aug 25 '18 at 08:50
  • 1
    I typed this on my iPhone. It’s hard not to reach the same conclusion if your right and haven’t seen an existing answer. The goal is to help the op. I would hope simple syntax errors on a 10 char bit of code could be overseen by the op. – atoms Aug 25 '18 at 08:52
  • It would help if you added anything about string parsing, e.g difference in PHP using single quotes/double quotes ;) – dbf Aug 25 '18 at 08:59
  • Those quotes are cancer. – Deepak Kamat Aug 25 '18 at 10:13
0

bydefault when you sending some data on server it will be encoded, to decrypt the data on other side you must use php urldecode() function as

echo urldecode("#1234\\y");// output:#1234\y
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70