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 "
< <
> >
& &
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 (' " ', ' "');
str = str.replace (' < ', ' < ');
str = str.replace (' > ', ' > ');
str = str.replace (' & ', ' & ');
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}";