Khi tôi chạy một cụm từ chứa hai trích dẫn thông qua hàm này, nó thay thế các trích dẫn bằng dấu ngoặc kép.
Tôi muốn loại bỏ hoàn toàn chúng [cũng là trích dẫn đơn]. Làm thế nào tôi có thể thay đổi chức năng để làm điều đó?
function string_sanitize[$s] {
$result = preg_replace["/[^a-zA-Z0-9]+/", "", $s];
return $result;
}
Update:
Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
hỏi ngày 28 tháng 3 năm 2011 lúc 3:12Mar 28, 2011 at 3:12
Scott Bscott bScott B
37.6K64 Huy hiệu vàng157 Huy hiệu bạc262 Huy hiệu Đồng64 gold badges157 silver badges262 bronze badges
1
Tôi sẽ không gọi chức năng đó string_sanitize[]
, vì nó sai lệch. Bạn có thể gọi nó là strip_non_alphanumeric[]
.
Chức năng hiện tại của bạn sẽ loại bỏ bất cứ thứ gì không phải là chữ cái trên hoặc chữ thường hoặc một số.
Bạn có thể tước chỉ '
và
Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
0 với ...$str = str_replace[array['\'', '"'], '', $str];
Justin Ethier
129K51 Huy hiệu vàng226 Huy hiệu bạc281 Huy hiệu Đồng51 gold badges226 silver badges281 bronze badges
Đã trả lời ngày 28 tháng 3 năm 2011 lúc 3:23Mar 28, 2011 at 3:23
Alexalexalex
467K197 Huy hiệu vàng865 Huy hiệu bạc975 Huy hiệu Đồng197 gold badges865 silver badges975 bronze badges
1
Có vẻ như chuỗi ban đầu của bạn có các ký tự HTML cho
Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
0 [Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
2] vì vậy khi bạn cố gắng vệ sinh nó, bạn chỉ cần xóa Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
3 và Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
4, để lại phần còn lại của chuỗi Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
5.---EDIT---
Có lẽ cách dễ nhất để loại bỏ các ký tự số không alpha sẽ là giải mã các ký tự HTML bằng html_entity_decode, sau đó chạy nó qua biểu thức thông thường. Vì, trong trường hợp này, bạn sẽ không nhận được bất cứ thứ gì cần được mã hóa lại, sau đó bạn không cần phải thực hiện htmlentity, nhưng đáng nhớ rằng bạn có dữ liệu HTML và giờ đây bạn có dữ liệu không được mã hóa.
Eg:
function string_sanitize[$s] {
$result = preg_replace["/[^a-zA-Z0-9]+/", "", html_entity_decode[$s, ENT_QUOTES]];
return $result;
}
Lưu ý rằng
Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
6 đánh dấu hàm thành "... chuyển đổi cả trích dẫn kép và đơn.".Đã trả lời ngày 28 tháng 3 năm 2011 lúc 3:21Mar 28, 2011 at 3:21
HamishhamishHamish
22.4K7 Huy hiệu vàng 50 Huy hiệu bạc67 Huy hiệu Đồng7 gold badges50 silver badges67 bronze badges
4
Tôi nghĩ rằng cuộc gọi preg_replace của bạn sẽ như thế này:
$result = preg_replace["/[^a-zA-Z0-9]+/", "", html_entity_decode[$s]];
Vui lòng xem Tham khảo HTML_ENTITY_DECODE để biết thêm chi tiết.
Đã trả lời ngày 28 tháng 3 năm 2011 lúc 3:25Mar 28, 2011 at 3:25
Anubhavaanubhavaanubhava
733K62 Huy hiệu vàng533 Huy hiệu bạc613 Huy hiệu Đồng62 gold badges533 silver badges613 bronze badges
Chức năng của bạn sử dụng biểu thức chính quy để loại bỏ bất kỳ ký tự nào khác với [A-A-Z0-9], do đó, nó chắc chắn sẽ loại bỏ bất kỳ "" hoặc ''
Chỉnh sửa: Chà, từ Hamish trả lời, tôi nhận ra chuỗi của bạn là một chuỗi HTML, để nó giải thích tại sao "[& quot] để được chuyển đổi thành" quote ". Bạn có thể xem xét thay thế
Example 1: This is 'the' first example
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all[] [function.preg-match-all]: Unknown modifier '0' in C
Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
7 bằng preg_replace hoặc htmlspecialchars_decode trước.Đã trả lời ngày 28 tháng 3 năm 2011 lúc 3:22Mar 28, 2011 at 3:22
Hoàng Longhoàn dàiHoàng Long
10,6K18 Huy hiệu vàng74 Huy hiệu bạc119 Huy hiệu đồng18 gold badges74 silver badges119 bronze badges
Để chắc chắn loại bỏ tất cả các loại trích dẫn [bao gồm cả những loại mà bên trái khác với các bên phải], tôi nghĩ nó phải giống như một cái gì đó giống như;
function string_sanitize[$s] {
$result = htmlentities[$s];
$result = preg_replace['/^["][.*]["]$/', "$2", $result];
$result = preg_replace['/^[«][.*][»]$/', "$2", $result];
$result = preg_replace['/^[“][.*][”]$/', "$2", $result];
$result = preg_replace['/^['][.*][']$/', "$2", $result];
$result = html_entity_decode[$result];
return $result;
}
Đã trả lời ngày 17 tháng 4 năm 2015 lúc 16:35Apr 17, 2015 at 16:35
gramogramogramo
1036 Huy hiệu Đồng6 bronze badges
Cách dễ dàng cho cả trích dẫn đơn và đôi :] và vẫn để lại một cái gì đó tương tự như nhìn vào.
$clean_string = str_replace['"', '``', str_replace["'", "`", $UserInput]];
Đã trả lời ngày 31 tháng 7 năm 2019 lúc 22:25Jul 31, 2019 at 22:25
2