Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3487666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:10:36+00:00 2026-05-18T11:10:36+00:00

What is the correct way in PHP to deal with decoding strings, such as

  • 0

What is the correct way in PHP to deal with decoding strings, such as these:

Test1 \\ Test2 \n Test3 \\n Test4 \abc

The desired output is:

Test \ Test2 (linebreak) Test3 \n Test4 abc

One thing I’ve tried was:

str_replace(array('\\\\','\\n','\\'), array('\\',"\n",''), $str);

But that doesn’t work, because it will run the replacing twice, which causes:

\\n

To be decoded as a linebreak anyway.

So I was thinking something like this:

$offset = 0;
$str = 'Test1 \\\\ Test2 \\n Test3 \\\\n Test4 \\abc';
while(($pos = strpos($str,'\\', $offset)) !== false) {

  $char = $str[$pos+1];
  if ($char=="n" || $char=="N") {
     // Insert a newline and eat 2 characters
     $str = substr($str,0,$pos-1) . "\n" . substr($str,$pos+2);
  } else {
     // eat slash
     $str = substr($str,0,$pos-1) . substr($str,$pos+1);
  }
  $offset=$pos+1;

}

This seems to work, but I was wondering if there’s maybe a built-in that does exactly this and I completely missed it, or a better/more compact way altogether to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:10:37+00:00Added an answer on May 18, 2026 at 11:10 am

    stripcslashes() almost works, except that it won’t recognize \a and skips it 🙁

    $str = 'Test1 \\\\ Test2 \\n Test3 \\\\n Test4 \\abc';
    echo stripcslashes($str);
    

    outputs this…

    Test1 \ Test2 
     Test3 \n Test4 bc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which is the most semantically correct way to get/set variables when using OO PHP?
What is the correct way to check if bit field is turn on -
What's the way the logout hashes are usually handled in php? on a lot
I am a little perplexed by the mysqli interface in PHP. When I set
I'm using jquery and php for some async job. I'm using $.ajax with beforesend
I am developing an iphone app that uses a php web service for all
How would you display / Echo pictures with a name underneeth in the order
Good morning. I currently have this little rule in my .htaccess RewriteEngine On RewriteBase
I have a page where ID is generated dynamically and be fetch from database
Let me explain what I mean in my title. Let's say, that for example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.