1

I know that javascript by default ignores the backslash \ character. I am trying to write 2 methods both in PHP & Javascript which methods should give the same output.

Example : The following is the original string ".\+*?[^]$(){}=!<>|:-"

After reading every character in the original String i got the below results :

  1. In PHP : ".\+*?[^]$(){}=!<>|:-"

The escaped string is : ".\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\-"

The descaped string is : ".\+*?[^]$(){}=!<>|:-" (like the original)

  1. In Javascript : .+*?[^]$(){}=!<>|:- (lost backslash \ character)

The escaped string is : "\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\-" (not the same with PHP because lost the backslash \ character at reading method)

The descaped string is : ".+*?[^]$(){}=!<>|:-" (with losted backslash \ character)

How can I get the same escaped - descaped output of both PHP & JS for any given String including backslash \ character?

Below is the code in both PHP & JS

  • PHP

     class Constants {
     /* List of following special characters : [.\+*?[^]$(){}=!<>|:-] */
    
     public static $ESCAPEDCHARS = [".", "\\", "+", "*", "?", "[", "^", "]", "$", "(", ")", "{", "}", "=", "!", "<", ">", "|", ":", "-"];
     public static $SLASH = "\\";
    
     public static function escapedArrayHasChar($char) {
         foreach (Constants::$ESCAPEDCHARS as $value) {
             if (strcmp($value, $char) === 0) {
                 return true;
             }
         }
         return false;
     }}
    
     class Tools {
     public static function escaped($str) {
         $length = strlen($str);
         $result = "";
         for ($index = 0; $index < $length; $index++) {
             if (Constants::escapedArrayHasChar($str[$index])) {
                 $result .= Constants::$SLASH . $str[$index];
             } else {
                 $result .= $str[$index];
             }
         }
         return $result;
     }
    
     public static function descaped($str) {
     $length = strlen($str);
     $result = "";
     for ($index = 0; $index < $length - 1; $index++) {
         if (strcmp(Constants::$SLASH, $str[$index]) === 0 && Constants::escapedArrayHasChar($str[$index + 1])) {
             $result .= $str[$index + 1];
             $index++;
         } else {
             $result .= $str[$index];
         }
         if ($index === $length - 2) {
             $result .= $str[$index + 1];
         }
     }
     return $result;
     }}
    
  • JAVASCRIPT

     $(function () {
         /*List of following special characters : [.\+*?[^]$(){}=!<>|:-] */
     var ESCAPEDCHARS = [".", "\\", "+", "*", "?", "[", "^", "]", "$", "(", ")", "{", "}", "=", "!", "<", ">", "|", ":", "-"];
     var SLASH = "\\", DOUBLESLASH = "\\\\";
    
     $.equals = function (str1, str2) {return str1 === str2;};
    
     $.escapedArrayHasChar = function (cr) {
     var index = 0;
     for (index = 0; index< ESCAPEDCHARS.length; index++) {
         if ($.equals(ESCAPEDCHARS[index], cr)) {
             return true;
         }
     }
     return false;
     };
    
     $.escaped = function (str) {
     var length = str.length;
     var result = "", index = 0;
     for (index = 0; index < length; index++) {
         if ($.escapedArrayHasChar(str[index])) {
             result += SLASH + str[index];
         } else {
             result += str[index];
         }
     }
     return result;
     };
    
     $.descaped = function (str) {
     var length = str.length;
     var result = "", index = 0;
     for (index = 0; index < length - 1; index++) {
         if ($.equals(SLASH, str[index]) && $.escapedArrayHasChar(str[index + 1])){
             result += str[index + 1];
             index++;
         }else {
             result += str[index];
         }
         if (index === length - 2) {
             result += str[index + 1];
         }
     }
     return result;
     };
     });
    

I think that the only solution is to ensure that 2 backslashes are imported into any string that needs to be checked. If the string has 2 backslashes for \ character both PHP & JS giving the same output.

1 Answer 1

1

Well, that seems like a lot of code to deal with the issue - I would probably go for encoding. For example, in base64 encoding, that string is: "LlwrKj9bXl0kKCl7fT0hPD58Oi0=". In php, base64_decode will return your string, and in javascript atob will return it as well. In the general case, you can create the encoding with base64_encode and in javascript with btoa.

Sign up to request clarification or add additional context in comments.

4 Comments

This not solve the problem The base64 return LlwrKj9bXl0kKCl7fT0hPD58Oi0= The btoa return LisqP1teXSQoKXt9PSE8Pnw6LQ== Again the btoa function ignore the backslash character because js by default escape this character. The problem is still here. The only solution I found is to ensure that the input string has \\ 2 backslashes for every \ 1 backslash.
@Stefanidis Sorry - I guess I missed what your actual objective is. The string itself is inherently ".\+*?[^]$(){}=!<>|:-". The fact that its represented in a quoted string differently in JS than PHP shouldn't matter for most purposes. Encoding solves the problem of getting one string from one language to the other without caring about how to escape it - you encode it in one language, and decode it in the other. If you are looking for some other purpose, maybe you could clarify in the posting.
In the whole string scanning with PHP if there is any alone \ backslash character without following \ backslash should be ignored. With this, the output is the same but the alone \ backslashes don't return back to the original string. Thus no solution found to get back the original string.
Finally i found the solution with String.raw js function. var original = String.raw.\+*?[^]$(){}=!<>|:-; And no more backslashes escaped so this is the solution Everything now works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.