php - Parse a variable as if it were a string literal -


i have string in variable don't control.

$foo = 'hey\nman';

(notice single quotes, it's literal \n not new line character)

now have foo parsed if inside double quotes instead of single. convert literal escape characters corresponding ascii characters. of /r, /n, /b, /t, etc.

i have access $foo can't change code assigns it's value it. option regex can ugly many number of different characters possible. how neatly php handles , wondering if can leverage it.

you can use eval:

$foo = 'hey\nman'; eval ('$b = "' . $foo . '";'); var_dump($b); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -