Hướng dẫn how remove single quotes and double quotes from string in php? - Làm thế nào để loại bỏ dấu ngoặc kép và dấu ngoặc kép khỏi chuỗi trong php?

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

Hướng dẫn how remove single quotes and double quotes from string in php? - Làm thế nào để loại bỏ dấu ngoặc kép và dấu ngoặc kép khỏi chuỗi trong php?

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ỉ '

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); 

Hướng dẫn how remove single quotes and double quotes from string in php? - Làm thế nào để loại bỏ dấu ngoặc kép và dấu ngoặc kép khỏi chuỗi trong php?

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

Hướng dẫn how remove single quotes and double quotes from string in php? - Làm thế nào để loại bỏ dấu ngoặc kép và dấu ngoặc kép khỏi chuỗi trong php?

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

Hướng dẫn how remove single quotes and double quotes from string in php? - Làm thế nào để loại bỏ dấu ngoặc kép và dấu ngoặc kép khỏi chuỗi trong php?

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

Làm thế nào để bạn xóa các trích dẫn kép đơn từ một chuỗi?

Để xóa các dấu ngoặc kép khỏi một chuỗi trong javascript, hãy sử dụng phương thức String.replace ().Bạn có thể nhắm mục tiêu các dấu ngoặc kép với regex và thay thế chúng bằng giá trị null.use the String. replace() method. You can target the quotation marks with RegEx and replace them with a null value.

Làm thế nào để bạn xóa dấu ngoặc kép kép khỏi một chuỗi?

Để xóa trích dẫn kép ngay từ đầu và đầu của chuỗi, chúng ta có thể sử dụng biểu thức thông thường cụ thể hơn: chuỗi kết quả = input.replaceall ("^\" | \ "$", "");Sau khi thực hiện ví dụ này, các lần xuất hiện của các trích dẫn kép ở đầu hoặc ở cuối chuỗi sẽ được thay thế bằng các chuỗi trống.String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

Làm cách nào để xóa một trích dẫn từ một chuỗi?

Xóa các trích dẫn đơn từ chuỗi..
thay thế tất cả( "'" , "" );.
thay thế tất cả( "\'" , "" );.
thay thế tất cả( "[\']" , "" );.

Làm cách nào để loại bỏ một báo giá trong PHP?

Hãy thử điều này: str_replace ('"'," ", $ String); str_replace (" '"str_replace('"', "", $string); str_replace("'", "", $string); Otherwise, go for some regex, this will work for html quotes for example: preg_replace("/