Bất kỳ ai có thể vui lòng chỉ cho tôi làm thế nào tôi có thể thay thế tất cả các ký tự đặc biệt trong một chuỗi ngoại trừ dấu gạch dưới và các ký hiệu khoảng thời gian. Và tôi cũng đã cố gắng hiểu cách thực hiện điều này thay thế các mẫu đọc hướng dẫn PHP
và nó rất khó hiểu cho người mới bắt đầu Giống như tôi, vậy có bất kỳ tài liệu hoặc hướng dẫn nào khác được thực hiện dễ dàng cho người mới bắt đầu để tôi không phải đăng một câu hỏi khác như thế này và mọi người gặp rắc rối mỗi khi tôi muốn sử dụng chức năng này?
preg_replace['/[^a-zA-Z0-9]/', '', $string];
Đây là những gì tôi có nó thay thế tất cả các nhân vật đặc biệt nhưng tôi muốn ngoại trừ _
và
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
0.Đã hỏi ngày 22 tháng 1 năm 2014 lúc 7:09Jan 22, 2014 at 7:09
1
Đặt _
và
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
0 cho tập hợp các ký tự bị phủ định [$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
3]:$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
Bạn không nên bỏ qua
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
4 vì $string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
5 Trả về chuỗi thay thế. Nó không thay đổi chuỗi tại chỗ.Đã trả lời ngày 22 tháng 1 năm 2014 lúc 7:12Jan 22, 2014 at 7:12
Falsetrufalsetrufalsetru
343K57 Huy hiệu vàng684 Huy hiệu bạc608 Huy hiệu đồng57 gold badges684 silver badges608 bronze badges
4
Bạn có thể sử dụng một số tiện ích bộ lọc PHP như Purifier [để đặt danh sách trắng cho đầu vào] ...
Tuy nhiên, chúng tôi muốn đề nghị bạn học Regex!
Đã trả lời ngày 22 tháng 1 năm 2014 lúc 7:23Jan 22, 2014 at 7:23
Jiangjiangjiang
Phù hiệu bằng đồng 17044 bronze badges
Trong bài viết này, chúng ta sẽ thấy cách loại bỏ các ký tự đặc biệt khỏi các chuỗi trong PHP, cùng với việc hiểu được việc thực hiện của họ thông qua các ví dụ. Chúng tôi đã đưa ra một chuỗi và chúng tôi cần xóa các ký tự đặc biệt khỏi chuỗi Str trong PHP, vì điều này, chúng tôi có các phương thức sau trong PHP:str in PHP, for this, we have the following methods in PHP:
Sử dụng phương thức str_replace []: Phương thức str_replace [] được sử dụng để loại bỏ tất cả các ký tự đặc biệt khỏi chuỗi str str str bằng cách thay thế các ký tự này bằng không gian trắng [phạm lỗi].str_replace[] Method: The str_replace[] methodis used to remove all the special characters from the given string str by replacing these characters with the white space [” “].
Syntax::
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]
Ví dụ: Ví dụ này minh họa việc sử dụng hàm str_replace [] để loại bỏ các ký tự đặc biệt khỏi chuỗi.This example illustrates the use of the str_replace[] function to remove the special characters from the string.
PHP
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
6$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
8 $string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
9str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]1
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]3
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]4
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]5
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]6
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]7
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]6
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]9____________
Example to remove the Special Char1
Example to remove the Special Char2
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
Example to remove the Special Char4
Example to remove the Special Char0
Example to remove the Special Char6
Example to remove the Special Char0
Example to remove the Special Char8
Example to remove the Special Char0
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]1
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]2____
Example to remove the Special Char0
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]7
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]3
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
Example to remove the Special Char1
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]4
Example to remove the Special Char5
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7Example to remove the Special Char8
Example to remove the Special Char9
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]5
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7preg_replace[ $pattern, $replacement, $subject, $limit, $count ]3
Example to remove the Special Char8
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
preg_replace[ $pattern, $replacement, $subject, $limit, $count ]6
Output:
Example to remove the Special Char
Sử dụng phương thức str_ireplace []: Phương thức str_ireplace [] được sử dụng để loại bỏ tất cả các ký tự đặc biệt khỏi chuỗi str str str bằng cách thay thế các ký tự này bằng không gian trắng [phạm lỗi]. Sự khác biệt giữa str_replace và str_ireplace là str_ireplace không nhạy cảm với trường hợp.str_ireplace[] Method: The str_ireplace[] method is used to remove all the special characters from the given string str by replacing these characters with the white space [” “]. The difference between str_replace and str_ireplace is that str_ireplace is case-insensitive.
Syntax::
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]
Ví dụ: Ví dụ này minh họa việc sử dụng phương thức str_ireplace [] để xóa tất cả các ký tự đặc biệt khỏi chuỗi.This example illustrates the use of the str_ireplace[] method to remove all the special characters from the string.
PHP
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
6$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
8 $string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
9str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]1
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]3
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]4
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]5
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]6
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]7
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]6
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]9____________
Example to remove the Special Char1
Example to remove the Special Char2
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
Example to remove the Special Char4
Example to remove the Special Char0
Example to remove the Special Char6
Example to remove the Special Char0
Example to remove the Special Char8
Example to remove the Special Char0
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]1
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]2____
Example to remove the Special Char0
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]7
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]3
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
Example to remove the Special Char1
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]4
Example to remove the Special Char5
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7Example to remove the Special Char8
Example to remove the Special Char9
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]5
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7preg_replace[ $pattern, $replacement, $subject, $limit, $count ]3
Example to remove the Special Char8
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
preg_replace[ $pattern, $replacement, $subject, $limit, $count ]6
Output:
Example to remove the Special Char
Example to remove the Special Char
preg_replace[] Method: The preg_replace[] method is used to perform a regular expression for search and replace the content.
Syntax::
preg_replace[ $pattern, $replacement, $subject, $limit, $count ]
Sử dụng phương thức str_ireplace []: Phương thức str_ireplace [] được sử dụng để loại bỏ tất cả các ký tự đặc biệt khỏi chuỗi str str str bằng cách thay thế các ký tự này bằng không gian trắng [phạm lỗi]. Sự khác biệt giữa str_replace và str_ireplace là str_ireplace không nhạy cảm với trường hợp.This example illustrates the use of the preg_replace[] method where the particular or all the matched pattern found in the input string will be replaced with the substring or white space.
PHP
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
6$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
8 $string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
9str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]1
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]3
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]4
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]5
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]6
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]7
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]6
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]9____________
Example to remove the Special Char1
Example to remove the Special Char2
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]2
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]7
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]3
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7Example to remove the Special Char1
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]4
Example to remove the Special Char5
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7Example to remove the Special Char8
Example to remove the Special Char9
str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]0
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]5
$string = preg_replace['/[^a-zA-Z0-9_.]/', '', $string];
7preg_replace[ $pattern, $replacement, $subject, $limit, $count ]3
Example to remove the Special Char8
str_ireplace[ $searchVal, $replaceVal, $subjectVal, $count ]9
preg_replace[ $pattern, $replacement, $subject, $limit, $count ]6
Output:
Example to remove the Special Char
Sử dụng phương thức str_ireplace []: Phương thức str_ireplace [] được sử dụng để loại bỏ tất cả các ký tự đặc biệt khỏi chuỗi str str str bằng cách thay thế các ký tự này bằng không gian trắng [phạm lỗi]. Sự khác biệt giữa str_replace và str_ireplace là str_ireplace không nhạy cảm với trường hợp.