Hướng dẫn php array string replace - thay thế chuỗi mảng php

(Php 4, Php 5, Php 7, Php 8)

str_replace - Thay thế tất cả các lần xuất hiện của chuỗi tìm kiếm bằng chuỗi thay thếReplace all occurrences of the search string with the replacement string

Sự mô tả

str_replace (& nbsp; & nbsp; & nbsp; & nbsp; mảng | chuỗi ________ 0, & nbsp; & nbsp; & nbsp; & nbsp; mảng &$count = null): Chuỗi | Mảng(
    array|string $search,
    array|string $replace,
    string|array $subject,
    int &$count = null
): string|array

Nếu bạn không cần thay thế các quy tắc thay thế (như biểu thức thông thường), bạn nên sử dụng chức năng này thay vì preg_replace ().preg_replace().

Thông số

Nếu searchreplace là mảng, thì str_replace () lấy giá trị từ mỗi mảng và sử dụng chúng để tìm kiếm và thay thế trên subject. Nếu replace có ít giá trị hơn search, thì một chuỗi trống được sử dụng cho phần còn lại của các giá trị thay thế. Nếu search là một mảng và replace là một chuỗi, thì chuỗi thay thế này được sử dụng cho mọi giá trị của search. Converse sẽ không có ý nghĩa, mặc dù.str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search. The converse would not make sense, though.

Nếu search hoặc replace là mảng, các yếu tố của chúng được xử lý trước để kéo dài.

search

Giá trị đang được tìm kiếm, còn được gọi là kim. Một mảng có thể được sử dụng để chỉ định nhiều kim.

replace

Giá trị thay thế thay thế các giá trị search được tìm thấy. Một mảng có thể được sử dụng để chỉ định nhiều thay thế.

subject

Chuỗi hoặc mảng đang được tìm kiếm và thay thế, còn được gọi là Haystack.

Nếu subject là một mảng, thì việc tìm kiếm và thay thế được thực hiện với mọi mục nhập của subject và giá trị trả về cũng là một mảng.

$subject1

Nếu được thông qua, điều này sẽ được đặt thành số lượng thay thế được thực hiện.

Trả về giá trị

Hàm này trả về một chuỗi hoặc một mảng với các giá trị được thay thế.

Ví dụ

Ví dụ #1 STR_ROPLE () Ví dụstr_replace() examples

$subject2

Ví dụ #2 Ví dụ về tiềm năng str_replace () gotchasstr_replace() gotchas

$subject3

Ghi chú

Lưu ý: Hàm này an toàn cho nhị phân.: This function is binary-safe.

Thận trọng

Lệnh thay thế Gotcha

Vì str_replace () thay thế từ trái sang phải, nó có thể thay thế một giá trị được chèn trước đó khi thực hiện nhiều thay thế. Xem thêm các ví dụ trong tài liệu này.str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.

Ghi chú::

Hàm này là nhạy cảm trường hợp. Sử dụng str_ireplace () để thay thế không nhạy cảm trường hợp.str_ireplace() for case-insensitive replace.

Xem thêm

  • str_ireplace () - Phiên bản không nhạy cảm trường hợp của str_replace
  • SPERTR_REPLACE () - Thay thế văn bản trong một phần của chuỗi
  • preg_replace () - Thực hiện tìm kiếm biểu thức thông thường và thay thế
  • strtr () - dịch các ký tự hoặc thay thế chuỗi con

Nikolaz Dot Tang tại Hotmail Dot Com ¶

12 năm trước

$subject4

$subject5

$subject6

$subject7

moostende tại gmail dot com ¶

11 năm trước

$subject8

$subject9

&$count0

&$count1

Alberto Lepe

13 năm trước

&$count2

&$count3

&$count4

&$count5

&$count6

&$count1

Wes Foster ¶

12 năm trước

&$count8

&$count9

null0

null1

moostende tại gmail dot com ¶

11 năm trước

null2

null3

&$count1

Alberto Lepe

13 năm trước

null5

null6

null7

&$count1

Wes Foster ¶

13 năm trước

null9

search0

search1

search2

search3

Wes Foster ¶

Ravenswd tại Gmail Dot Com ¶

search4

search5

search6

search7

search8

&$count1

4 năm trước

11 năm trước

replace0

replace1

replace2

replace3

replace4

replace5

Alberto Lepe

12 năm trước

replace6

replace7

&$count1

moostende tại gmail dot com ¶

12 năm trước

replace9

subject0

subject1

&$count1

moostende tại gmail dot com ¶

11 năm trước

subject3

subject4

subject5

&$count1

Alberto Lepe

13 năm trước

subject7

Wes Foster ¶

13 năm trước

subject8

subject9

replace0

replace1

&$count1

Wes Foster ¶

13 năm trước

replace3

replace4

replace5

replace6

replace7

replace8

replace9

search0

search1

&$count1

Wes Foster ¶

12 năm trước

search3

search4

&$count1

moostende tại gmail dot com ¶

11 năm trước

search6

Alberto Lepe

11 năm trước

search7

search8

search9

Alberto Lepe

13 năm trước

$replace00

$replace01

$replace02

$replace03

$replace04

$replace05

&$count1

Wes Foster ¶

11 năm trước

$replace07

$replace08

$replace09

&$count1

Alberto Lepe

13 năm trước

$replace11

Wes Foster ¶

13 năm trước

$replace12

$replace13

$replace14

$replace15

$replace16

$replace17

$replace18

$replace19

$replace20

$replace21

$replace22

$replace23

$replace24

$replace25

$replace26

$replace27

$replace28

$replace29

$replace30

$replace31

$replace32

&$count1

Wes Foster ¶

11 năm trước

$replace34

$replace35

$replace36

$replace37

&$count1

Alberto Lepe

11 năm trước

$replace39

$replace40

$replace41

$replace42

$replace43

&$count1

Alberto Lepe

Ravenswd tại Gmail Dot Com ¶

$replace45

$replace46

&$count1

Christof dot rieger tại r-tron dot de ¶

10 năm trước

$replace48

$replace49

$replace50

&$count1

Flame2000 tại mail dot ru ¶

6 năm trước

$replace52

$replace53

$replace54

$replace55

Aschmidt tại Anamera Dot Net

4 năm trước

$replace56

$replace57

$replace58

$replace59

&$count1

vuabid tại hotmail dot com

5 năm trước

$replace61

$replace62

$replace63

$replace64

$replace65

&$count1

Christian Dot Reinecke tại Web Dot de ¶

12 năm trước

$replace67

Oyedele Hammed Horlah - Itz Dot Harmid tại Gmail Dot Com ¶

5 năm trước

$replace68

$replace69

$replace70

&$count1

Christian Dot Reinecke tại Web Dot de ¶

12 năm trước

$replace72

$replace73

&$count1

Oyedele Hammed Horlah - Itz Dot Harmid tại Gmail Dot Com ¶

Hyperzlib tại Outlook Dot Com ¶

$replace75

$replace76

&$count1

2 năm trước

12 năm trước

$replace78