Bộ lọc nội dung để loại bỏ các dấu gạch chéo không cần thiết
nội dung
Nội dung sửa đổi
chuỗi Nội dung đã loại bỏ dấu gạch chéo.
Tài liệu. wp-admin/includes/upgrade.php. Xem tất cả tài liệu tham khảo
function deslash( $content ) { // Note: \\\ inside a regex denotes a single backslash. /* * Replace one or more backslashes followed by a single quote with * a single quote. */ $content = preg_replace( "/\\\+'/", "'", $content ); /* * Replace one or more backslashes followed by a double quote with * a double quote. */ $content = preg_replace( '/\\\+"/', '"', $content ); // Replace one or more backslashes with one backslash. $content = preg_replace( '/\\\+/', '\\', $content ); return $content; }
Bạn phải đăng nhập trước khi có thể đóng góp ghi chú hoặc phản hồi
Hướng dẫn này giới thiệu cách thay thế dấu gạch chéo ngược đơn (_______7_______) bằng dấu gạch chéo ngược kép (\\) trong Java
Dấu gạch chéo ngược đơn và kép được sử dụng để tạo đường dẫn tệp hoặc thư mục trong hệ điều hành. Java cũng tuân theo các kiểu này để định vị bất kỳ tài nguyên nào trong bộ nhớ. Chuỗi Java sử dụng dấu gạch chéo ngược nhân đôi do quy ước. Trong bài viết này, chúng ta sẽ học cách thay thế dấu gạch chéo ngược đơn bằng dấu gạch chéo ngược kép. Hãy bắt đầu với một số ví dụ
Thay thế một dấu gạch chéo ngược đơn (\) bằng một dấu gạch chéo ngược kép (\\) trong Java
Trong ví dụ này, chúng tôi đã sử dụng phương thức replace() của lớp String để thay thế dấu gạch chéo ngược đơn bằng dấu gạch chéo ngược kép. Phương thức này trả về một đối tượng Chuỗi đã sửa đổi mới. Xem ví dụ bên dưới
public class SimpleTesting{ public static void main(String[] args){ String msg = "Hello \\ my name is delft"; System.out.println(msg); String newmsg = msg.replace("\\", "\\\\"); System.out.println(newmsg); } }
đầu ra
Hello \ my name is delft Hello \\ my name is delft
Thay thế một dấu gạch chéo ngược đơn (\) bằng một dấu gạch chéo ngược kép (\\) bằng cách sử dụng phương thức Hello \ my name is delft Hello \\ my name is delft 2
Đây là một giải pháp khác mà bạn có thể sử dụng để thay thế dấu gạch chéo ngược. Ở đây, chúng tôi đã sử dụng phương thức Hello \ my name is delft Hello \\ my name is delft 2 hoạt động tốt và trả về một đối tượng Chuỗi mới. Xem ví dụ bên dưới
spamdunk at home dot com, your way is dangerous on PostgreSQL (and presumably MySQL). You're quite correct that ANSI SQL specifies using ' to escape, but those databases also support \ for escaping (in violation of the standard, I think). Which means that if they pass in a string that includes a "\'", you expand it to "\'''" (an escaped quote followed by a non-escaped quote. WRONG! Attackers can execute arbitrary SQL to drop your tables, make themselves administrators, whatever they want.)
The best way to be safe and correct is to:
- don't use magic quotes; this approach is bad. For starters, that's making the assumption that you will be using your input in a database query, which is arbitrary. (Why not escape all "<"s with "<"s instead? Cross-site scripting attacks are quite common as well.) It's better to set up a way that does whatever escaping is correct for you when you use it, as below:
- when inserting into the database, use prepared statements with placeholders. For example, when using PEAR DB:
$stmt = $dbh->prepare('update mb_users set password = ? where username = ?');
$dbh->execute($stmt, array('12345', 'bob'));
?>
Notice that there are no quotes around the ?s. It handles that for you automatically. It's guaranteed to be safe for your database. (Just ' on oracle, \ and ' on PostgreSQL, but you don't even have to think about it.)
Plus, if the database supports prepared statements (the soon-to-be-released PostgreSQL 7.3, Oracle, etc), several executes on the same prepare can be faster, since it can reuse the same query plan. If it doesn't (MySQL, etc), this way falls back to quoting code that's specifically written for your database, avoiding the problem I mentioned above.
(Pardon my syntax if it's off. I'm not really a PHP programmer; this is something I know from similar things in Java, Perl, PL/SQL, Python, Visual Basic, etc.)