PHP thay thế số bằng chữ cái

Trong hướng dẫn này, chúng ta sẽ xem Cách loại bỏ mọi thứ khỏi chuỗi trừ số bằng PHP. Có thể sử dụng hàm php preg_replace[] với biểu thức chính quy và hàm filter_var[] với bộ lọc php FILTER_SANITIZE_NUMBER_INT cho việc này

Mục lục

  • hàm preg_replace[]
  • Biểu hiện thông thường
  • hàm filter_var[]
  • FILTER_SANITIZE_NUMBER_INT Bộ lọc
  • Thêm hướng dẫn PHP

hàm preg_replace[]

Hàm preg_replace[] là một hàm php tích hợp được sử dụng để thay thế một mẫu trong chuỗi bằng chuỗi thay thế hoặc chuỗi con

Biểu hiện thông thường

Biểu thức chính quy là một chuỗi các ký tự chỉ định mẫu tìm kiếm. Cụm từ thông dụng được sử dụng với hàm preg_replace của php để thay thế mẫu tìm kiếm

Ví dụ về chức năng preg_replace[] với regex được đưa ra bên dưới

Example to remove the Special Char
36

đầu ra

123451 sẽ là đầu ra. Tất cả mọi thứ được thay thế ngoại trừ số

Ví dụ thứ hai về chức năng preg_replace[] với \D Metacharacter được đưa ra bên dưới

Example to remove the Special Char
37

đầu ra

123451 sẽ là đầu ra

hàm filter_var[]

filter_var[] Hàm lọc hoặc thay thế một chuỗi hoặc biến bằng bộ lọc đã chỉ định. Bộ lọc FILTER_SANITIZE_NUMBER_INT được sử dụng với hàm filter_var[] để thay thế tất cả các loại ký tự và bảng chữ cái từ chuỗi ngoại trừ số

FILTER_SANITIZE_NUMBER_INT Bộ lọc

Bộ lọc PHP FILTER_SANITIZE_NUMBER_INT là bộ lọc loại bỏ tất cả các ký tự và bảng chữ cái khỏi một chuỗi ngoại trừ các số và. + -

Trong bài viết này, chúng ta sẽ xem cách xóa các ký tự đặc biệt khỏi chuỗi trong PHP, cùng với việc hiểu cách thực hiện chúng 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, để làm điều này, chúng tôi có các phương thức sau trong PHP

Sử dụng Phương thức str_replace[]. Phương thức str_replace[] được sử dụng để xóa tất cả các ký tự đặc biệt khỏi chuỗi str đã cho bằng cách thay thế các ký tự này bằng khoảng trắng [” “]

cú pháp

str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]

Thí dụ. Ví dụ này minh họa việc sử dụng hàm str_replace[] để xóa các ký tự đặc biệt khỏi chuỗi

PHP




Example to remove the Special Char
38

Example to remove the Special Char
39
Example to remove the Special Char
40

Example to remove the Special Char
39
Example to remove the Special Char
0

 

Example to remove the Special Char
39
Example to remove the Special Char
2

Example to remove the Special Char
39
Example to remove the Special Char
4
Example to remove the Special Char
5
Example to remove the Special Char
6
Example to remove the Special Char
7

 

Example to remove the Special Char
8
Example to remove the Special Char
9

Example to remove the Special Char
8
Example to remove the Special Char
01

Example to remove the Special Char
8
Example to remove the Special Char
03
Example to remove the Special Char
04
Example to remove the Special Char
05
Example to remove the Special Char
06
Example to remove the Special Char
07
Example to remove the Special Char
06
Example to remove the Special Char
09
Example to remove the Special Char
0
Example to remove the Special Char
1
Example to remove the Special Char
0

Example to remove the Special Char
8
Example to remove the Special Char
4
Example to remove the Special Char
0
Example to remove the Special Char
6
Example to remove the Special Char
0
Example to remove the Special Char
8
Example to remove the Special Char
0
Example to remove the Special Char
90
Example to remove the Special Char
91
Example to remove the Special Char
92
Example to remove the Special Char
0
Example to remove the Special Char
6
Example to remove the Special Char
95

 

Example to remove the Special Char
8
Example to remove the Special Char
97

Example to remove the Special Char
8
Example to remove the Special Char
99
Example to remove the Special Char
03____11

Example to remove the Special Char
8
Example to remove the Special Char
3

 

Example to remove the Special Char
39
Example to remove the Special Char
5

Example to remove the Special Char
39
Example to remove the Special Char
6
Example to remove the Special Char
04
Example to remove the Special Char
9
Example to remove the Special Char
1

 

Example to remove the Special Char
39
Example to remove the Special Char
382

Example to remove the Special Char
39
Example to remove the Special Char
384
Example to remove the Special Char
385
Example to remove the Special Char
6
Example to remove the Special Char
95

 

Example to remove the Special Char
39____1389

Example to remove the Special Char
39
Example to remove the Special Char
391
Example to remove the Special Char
384
Example to remove the Special Char
1

Example to remove the Special Char
394

đầu ra

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 để xóa tất cả các ký tự đặc biệt khỏi chuỗi str đã cho bằng cách thay thế các ký tự này bằng khoảng trắng [” “]. Sự khác biệt giữa str_replace và str_ireplace là str_ireplace không phân biệt chữ hoa chữ thường

cú pháp

Example to remove the Special Char
0

Thí 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

PHP




Example to remove the Special Char
38

Example to remove the Special Char
39
Example to remove the Special Char
40

Example to remove the Special Char
39
Example to remove the Special Char
0

 

Example to remove the Special Char
39
Example to remove the Special Char
2

Example to remove the Special Char
39
Example to remove the Special Char
4
Example to remove the Special Char
5
Example to remove the Special Char
6
Example to remove the Special Char
406

 

Example to remove the Special Char
8
Example to remove the Special Char
408

Example to remove the Special Char
8
Example to remove the Special Char
01

Example to remove the Special Char
8
Example to remove the Special Char
03
Example to remove the Special Char
04
Example to remove the Special Char
394
Example to remove the Special Char
06
Example to remove the Special Char
07
Example to remove the Special Char
06
Example to remove the Special Char
09
Example to remove the Special Char
0
Example to remove the Special Char
1
Example to remove the Special Char
0

Example to remove the Special Char
8
Example to remove the Special Char
4
Example to remove the Special Char
0
Example to remove the Special Char
6
Example to remove the Special Char
0
Example to remove the Special Char
8
Example to remove the Special Char
0
Example to remove the Special Char
90
Example to remove the Special Char
91
Example to remove the Special Char
92
Example to remove the Special Char
0
Example to remove the Special Char
6
Example to remove the Special Char
95

 

Example to remove the Special Char
8
Example to remove the Special Char
16

Example to remove the Special Char
8
Example to remove the Special Char
99
Example to remove the Special Char
03____11

Example to remove the Special Char
8
Example to remove the Special Char
3

 

Example to remove the Special Char
39
Example to remove the Special Char
5

Example to remove the Special Char
39
Example to remove the Special Char
6
Example to remove the Special Char
04
Example to remove the Special Char
9
Example to remove the Special Char
1

 

Example to remove the Special Char
39
Example to remove the Special Char
382

Example to remove the Special Char
39
Example to remove the Special Char
384
Example to remove the Special Char
385
Example to remove the Special Char
6
Example to remove the Special Char
95

 

Example to remove the Special Char
39____1389

Example to remove the Special Char
39
Example to remove the Special Char
391
Example to remove the Special Char
384
Example to remove the Special Char
1

Example to remove the Special Char
394

đầu ra

Example to remove the Special Char

Sử dụng Phương thức preg_replace[]. Phương thức preg_replace[] được sử dụng để thực hiện một biểu thức chính quy để tìm kiếm và thay thế nội dung

cú pháp

Example to remove the Special Char
9

Thí dụ. Ví dụ này minh họa việc sử dụng phương thức preg_replace[] trong đó một mẫu cụ thể hoặc tất cả mẫu phù hợp tìm thấy trong chuỗi đầu vào sẽ được thay thế bằng chuỗi con hoặc khoảng trắng

PHP




Example to remove the Special Char
38

Example to remove the Special Char
39
Example to remove the Special Char
40

Example to remove the Special Char
39
Example to remove the Special Char
0

 

Example to remove the Special Char
39
Example to remove the Special Char
2

Example to remove the Special Char
39
Example to remove the Special Char
4
Example to remove the Special Char
5
Example to remove the Special Char
6
Example to remove the Special Char
406

 

Example to remove the Special Char
8
Example to remove the Special Char
57

Example to remove the Special Char
8
Example to remove the Special Char
01

Example to remove the Special Char
8
Example to remove the Special Char
03
Example to remove the Special Char
62
Example to remove the Special Char
63
Example to remove the Special Char
0
Example to remove the Special Char
92
Example to remove the Special Char
0
Example to remove the Special Char
6
Example to remove the Special Char
95

 

Example to remove the Special Char
8
Example to remove the Special Char
97

Example to remove the Special Char
8
Example to remove the Special Char
99
Example to remove the Special Char
03____11

Example to remove the Special Char
39
Example to remove the Special Char
3

 

Example to remove the Special Char
39
Example to remove the Special Char
5

Example to remove the Special Char
39
Example to remove the Special Char
6
Example to remove the Special Char
04
Example to remove the Special Char
9
Example to remove the Special Char
1

 

Example to remove the Special Char
39
Example to remove the Special Char
382

Example to remove the Special Char
39
Example to remove the Special Char
384
Example to remove the Special Char
385
Example to remove the Special Char
6
Example to remove the Special Char
95

 

Example to remove the Special Char
39____1389

Example to remove the Special Char
39
Example to remove the Special Char
391
Example to remove the Special Char
384
Example to remove the Special Char
1

Example to remove the Special Char
394

đầu ra

Example to remove the Special Char

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này

str_ thay thế[] là gì?

Định nghĩa và cách sử dụng. Hàm str_replace[] thay thế một số ký tự bằng một số ký tự khác trong chuỗi . Chức năng này hoạt động theo các quy tắc sau. Nếu chuỗi cần tìm là một mảng, nó sẽ trả về một mảng. Nếu chuỗi cần tìm là một mảng thì việc tìm và thay thế được thực hiện với mọi phần tử của mảng.

Làm cách nào để thay thế các giá trị trong PHP?

The str_replace[] là một hàm tích hợp sẵn trong PHP và được sử dụng để thay thế tất cả các lần xuất hiện của chuỗi tìm kiếm hoặc mảng của .

Làm cách nào để thay thế nhiều ký tự trong một chuỗi trong PHP?

Cách tiếp cận 1. Sử dụng các hàm str_replace[] và str_split[] trong PHP . Hàm str_replace[] dùng để thay thế nhiều ký tự trong một chuỗi và nó nhận ba tham số. Tham số đầu tiên là mảng ký tự cần thay thế.

Chủ Đề