0

Convert escaped characters/ whitespaces to plain text

I received data from another database and want to data as plain text.

For now it is using \n, \v,\e, \t, etc as escaped characters. But I want to use it as a text. I don't want to use str_replace, preg_replace. So how to achieve the solution for this

Output should be like below

Example:

echo $abc;

Output:

Abc\n\tasd
10
  • Most APIs return JSON, and json_decode() will convert the escaped characters. What API returns these literally and requires you to parse them? Commented Aug 31, 2022 at 6:40
  • Is there a way to assign php variable in single quote as single quote ignore escaped characters? Commented Aug 31, 2022 at 6:46
  • I apparently misunderstood the question. You don't want to parse the escape sequences, you want to see the escape sequences. So the returned data doesn't contain the escape sequences, it contains the actual control characters. Commented Aug 31, 2022 at 6:51
  • See addcslashes Commented Aug 31, 2022 at 6:52
  • Yes, you are right I want the same text as it is getting. Not want \n \t etc to work as escaped chars Commented Aug 31, 2022 at 6:53

1 Answer 1

2

Use addcslashes()

$abc = "foo\nbar\txyz";
 
echo addcslashes($abc, "\n\t\e\v\r");

DEMO

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

4 Comments

Its not working for \e
try addcslashes($string,"\x00..\x1f");
\e is represented as "\033", which is the same.
@AkJosh Did you add it to the second argument? That tells the function which characters it should replace (I linked to the documentation so you would understand how to use it).

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.