PHP convert double quoted string to single quoted string

Paresh Thummar

I know this question asked here many times.But That solutions are not useful for me. I am facing this problem very badly today.

// Case 1
$str = 'Test \300';  // Single Quoted String
echo json_encode(utf8_encode($str)) // output: Test \\300

// Case 2
$str = "Test \300"; // Double Quoted String
echo json_encode(utf8_encode($str)) // output: Test \u00c0

I want case 2's output and I have single quoted $str variable. This variable is filled from XML string parsing . And that XML string is saved in txt file.

(Here \300 is encoding of À (latin Charactor) character and I can't control it.)

Please Don't give me solution for above static string

Thanks in advance

deceze

This'll do:

$string = '\300';

$string = preg_replace_callback('/\\\\\d{1,3}/', function (array $match) {
    return pack('C', octdec($match[0]));
}, $string);

It matches any sequence of a backslash followed by up to three numbers and converts that number from an octal number to a binary string. Which has the same result as what "\300" does.

Note that this will not work exactly the same for escaped escapes; i.e. "\\300" will result in a literal \300 while the above code will convert it.

If you want all the possible rules of double quoted strings followed without reimplementing them by hand, your best bet is to simply eval("return \"$string\""), but that has a number of caveats too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP: Convert single-quoted string into double-quoted

From Dev

php: string is double-quoted, not single-quoted. How to fix?

From Dev

What is the difference between single quoted $'string' and double quoted $"string" in bash?

From Dev

What is the difference between single quoted $'string' and double quoted $"string" in bash?

From Dev

Converting environment variable from double quoted string to single quoted

From Dev

Which is the correct way of using core predefined constant PHP_EOL inside a double quoted string and single quoted string?

From Dev

Python str() double quoted string

From Dev

How to match string in single or double quoted using regex

From Dev

Multi-line, double quoted string triggers history expansion on subsequent single-quoted commands it gets piped to

From Dev

Different behaviour between double quoted and single quoted string when using urllib in python

From Dev

How to insert php variable inside a double quoted php string

From Dev

PHP Carbon add dynamic object property inside double quoted string

From Dev

Extract double quoted string content with Parboiled

From Dev

How to add a variable inside a double quoted string

From Dev

How to add a variable inside a double quoted string

From Dev

How to add a newline in a double quoted string

From Dev

Regular expression for pipe delimited and double quoted string

From Dev

Perl - DBI string incorrectly gets double quoted

From Dev

split a double quoted string in ruby on a backslash

From Dev

Retrieve single quoted string from database

From Dev

Single quoted string to uint in c#

From Dev

How to parse characters in a single-quoted string?

From Dev

How to parse characters in a single-quoted string?

From Dev

Replace single quoted string that is separated by comma

From Dev

How do i convert a string to a quoted variable

From Dev

How to convert embedded (quoted) json string to json

From Dev

How to convert embedded (quoted) json string to json

From Dev

Convert quoted string list to specific format

From Dev

pyparsing string of quoted names

Related Related

  1. 1

    PHP: Convert single-quoted string into double-quoted

  2. 2

    php: string is double-quoted, not single-quoted. How to fix?

  3. 3

    What is the difference between single quoted $'string' and double quoted $"string" in bash?

  4. 4

    What is the difference between single quoted $'string' and double quoted $"string" in bash?

  5. 5

    Converting environment variable from double quoted string to single quoted

  6. 6

    Which is the correct way of using core predefined constant PHP_EOL inside a double quoted string and single quoted string?

  7. 7

    Python str() double quoted string

  8. 8

    How to match string in single or double quoted using regex

  9. 9

    Multi-line, double quoted string triggers history expansion on subsequent single-quoted commands it gets piped to

  10. 10

    Different behaviour between double quoted and single quoted string when using urllib in python

  11. 11

    How to insert php variable inside a double quoted php string

  12. 12

    PHP Carbon add dynamic object property inside double quoted string

  13. 13

    Extract double quoted string content with Parboiled

  14. 14

    How to add a variable inside a double quoted string

  15. 15

    How to add a variable inside a double quoted string

  16. 16

    How to add a newline in a double quoted string

  17. 17

    Regular expression for pipe delimited and double quoted string

  18. 18

    Perl - DBI string incorrectly gets double quoted

  19. 19

    split a double quoted string in ruby on a backslash

  20. 20

    Retrieve single quoted string from database

  21. 21

    Single quoted string to uint in c#

  22. 22

    How to parse characters in a single-quoted string?

  23. 23

    How to parse characters in a single-quoted string?

  24. 24

    Replace single quoted string that is separated by comma

  25. 25

    How do i convert a string to a quoted variable

  26. 26

    How to convert embedded (quoted) json string to json

  27. 27

    How to convert embedded (quoted) json string to json

  28. 28

    Convert quoted string list to specific format

  29. 29

    pyparsing string of quoted names

HotTag

Archive