Denis Balan
Denis Balan

Reputation: 198

How to unescape string in php?

Given a string like this

$str = '\007\t\007\006\006\t\n\026\r\\\045';

format and output unescaped string so that \t - tabulation and so on.

I could solve this by using replacements, but maybe there is a better solution?

Upvotes: 1

Views: 2650

Answers (1)

Barmar
Barmar

Reputation: 780889

The best I can think of is:

eval('$str_unescaped = "' . str_replace('"', '\"', $str) . '";');

str_replace is needed in case the string contains any embedded double quotes. But it still gets an error if it contains an embedded \" sequence.

Upvotes: 1

Related Questions