Special character processing in thinkphp

Source: Internet
Author: User

Based on recent projects--there are problems with storage presentations for some special characters, and the solution is as follows:

Character encoding: "I conversion via thinkphp to "
< &lt;
> &gt;
& &amp;
You can restore if you use a template variable for direct reference, but you cannot use JS to assign a value. JS Restore function:
function Restore_str_js (str) {
var str = str.replace (' &quot; ', ' "');
str = str.replace (' &lt; ', ' < ');
str = str.replace (' &gt; ', ' > ');
str = str.replace (' &amp; ', ' & ');
str = str.replace (' char (') ', ' \ r \ n ');//restore newline character to page
return str;
}




Handling of line breaks:
TextArea sometimes the user inserts a line break, but there is an error when inserting into the database without processing.
PHP is stored in the database after processing:
Replaces a newline character with a special tag, which is restored when the page is displayed
function Filter_enter ($STR) {
$str = Str_replace (Array ("\ r \ n", "\ r", "\ n"), ' char ', $str); Replace the user input carriage return with a special character//textarea
return $str;
}


If read for JS: str = str.replace (' char ', ' \ r \ n '), and/or restore line break to show on page
If the template variable:
When a template directly references a variable, it is converted to a newline character.
function Restore_enter ($STR) {
$str = str_replace (' char ', ' <br/> ', $str);//restore newline character to page
return $str;
}




\ counter Slash processing:
The I function is not processed, but there is an error in the SQL query statement. The correct SQL statement:
SELECT * from ' a ' where Word is like '%\\ '; Result: aspert\
1 backslash \ To be converted to 2 backslash \ before query:
Converts \ One backslash to \ Two backslash for database query
function Filter_str ($STR) {
$str = Str_replace ("\", "\\\\", $str);
return $str;
}


If the database is stored as is, direct read is assigned to the template variable, and the presentation is problematic.
Convert \ backslash to & #92; (HTML entity) for display on page
function Restore_str ($STR) {
$str = Str_replace ("\", "& #92;", $STR);
return $str;
}


If read with JS:
PHP: $this->assign (' name ', Filter_str ($name)); In order to reverse the backslash or a backslash, followed and character Fulianqi to escape garbled
Js:var name = "{$name}";

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.