The method of realizing escape and unescape in JS in PHP _php skill

Source: Internet
Author: User
Tags exception handling php error php introduction php json

This article describes the PHP implementation of JS Escape and unescape method. Share to everyone for your reference, specific as follows:

Escape and unescape in JavaScript is it? The string is converted to Unicode hexadecimal encoding, the same as the JSON encoding format, so you can pass the Json_encode and Json_ in PHP Decode implements the PHP version of Escape and unescape.

The code is as follows:

<?php
function Escape ($str) {return
  str_replace (' \ \ ', '% ', substr (Json_encode ($STR), 1,-1));
}
function Unescape ($str) {return
  Json_decode (' "'. Str_replace ('% ', ' \ \ ', $str). ')
$str = '%u5f90%u5dde%u5e02%u811a%u672c%u4e4b%u5bb6 1234 ';
$str _escape = ' XUZHOU cloud Habitat Community 1234 ';
echo "$str <br>\n$str_escape<br>\n";
echo unescape ($STR), "<br>\n";
Echo Escape ($str _escape), "<br>\n";

The results of the operation are as follows:

%U5F90%U5DDE%U5E02%U811A%U672C%U4E4B%U5BB6 1234
Xuzhou cloud-dwelling community 1234 Xuzhou
cloud-Habitat Community 1234%U5F90%U5DDE%U5E02%u811a
%U672C%U4E4B%U5BB6 1234

More interested in PHP related content readers can view the site topics: "PHP JSON format data Operation tips Summary", "PHP error and Exception handling method summary", "PHP string (String) Usage summary", "PHP Array" Operation Techniques Encyclopedia, " Summary of PHP operation and operator usage, "Summary of PHP Network programming skills", "Introduction to PHP Basic Grammar", "PHP Introduction to Object-oriented Programming", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.