Hàm STR_replace() trong PHP là gì?

Bản tóm tắt. trong hướng dẫn này, bạn sẽ học cách sử dụng hàm PHP

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 để thay thế tất cả các lần xuất hiện của một chuỗi con bằng một chuỗi mới

Giới thiệu về hàm PHP str_replace()

Hàm PHP

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 trả về một chuỗi mới với tất cả các lần xuất hiện của một chuỗi con được thay thế bằng một chuỗi khác

Sau đây là cú pháp của hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 có các tham số sau

  • <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

    Code language: PHP (php)
    4 là một chuỗi con được thay thế
  • <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

    Code language: PHP (php)
    5 là chuỗi thay thế thay thế cho giá trị______04
  • <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

    Code language: PHP (php)
    7 là chuỗi đầu vào
  • <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

    Code language: PHP (php)
    8 trả về số lần thay thế mà hàm đã thực hiện.

    <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

    Code language: PHP (php)
    8 là tùy chọn

Nếu các đối số

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
4 và

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
5 là các mảng, thì ____ ______ sẽ lấy từng giá trị từ mảng

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
4 (từ trái sang phải) và thay thế nó bằng từng giá trị từ mảng

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
5

Nếu mảng

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
5 có ít phần tử hơn mảng

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
4, thì hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 sử dụng một chuỗi rỗng cho các giá trị thay thế

Nếu

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
4 là một mảng và

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
5 là một chuỗi, thì hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 sẽ thay thế mọi phần tử trong

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
4 bằng chuỗi

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
5

Lưu ý rằng

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 không thay đổi chuỗi đầu vào (

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
7). Nó trả về một mảng mới với

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
4 được thay thế bằng

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
5

Ví dụ hàm PHP <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello thereCode language: PHP (php)0

Hãy lấy một số ví dụ về việc sử dụng hàm PHP

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0

Ví dụ hàm PHP <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello thereCode language: PHP (php)0 đơn giản

Ví dụ sau sử dụng hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 để thay thế chuỗi

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
21 bằng

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
22 trong chuỗi

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
23

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)

đầu ra

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
6

Như được hiển thị trong đầu ra,

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 không thay đổi chuỗi đầu vào nhưng trả về một chuỗi mới với chuỗi con

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
21 được thay thế bằng chuỗi

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
22;

Ví dụ sau sử dụng hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 để thay thế chuỗi con

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
28 bằng chuỗi

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
29 trong chuỗi

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
30

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
4

đầu ra

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
2

Hàm PHP <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello thereCode language: PHP (php)0 với ví dụ đối số đếm

Ví dụ sau sử dụng hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 với đối số đếm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
3

Trong ví dụ này, hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 thay thế chuỗi con

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
34 bằng chuỗi

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
28 trong chuỗi

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
36

Số đếm trả về hai vì hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 chỉ thay thế chuỗi con

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
34 chứ không phải

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
22

Hàm PHP <?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello thereCode language: PHP (php)0 với nhiều ví dụ thay thế

Ví dụ sau sử dụng hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 để thay thế

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
82 bằng

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
83 và

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
84 bằng

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
85 trong chuỗi

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
86

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
8

đầu ra

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
4

Vì hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
0 thay thế từ trái sang phải nên nó có thể thay thế giá trị đã thay thế trước đó trong khi thực hiện nhiều lần thay thế. Ví dụ

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
6

Trong ví dụ này, táo chuỗi được thay thế bằng cam, cam được thay thế bằng chuối và chuối được thay thế bằng dâu tây. Chuỗi trả về là dâu tây

Hàm str_ireplace() trong PHP

Để tìm kiếm một chuỗi không phân biệt chữ hoa chữ thường và thay thế nó bằng một chuỗi thay thế, bạn sử dụng hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
88. Ví dụ

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
8

Trong ví dụ này, hàm

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
88 thay thế chuỗi con

<?php $str = 'Hello there'; $new_str = str_replace('Hello', 'Hi', $str); echo $new_str . '<br>'; // Hi there echo $str . '<br>'; // Hello there

Code language: PHP (php)
34 hoặc

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
22 bằng chuỗi

str_replace ( array|string $search , array|string $replace , string|array $subject , int &$count = null ) : string|array

Code language: PHP (php)
28

Công dụng của hàm Str_replace() trong PHP là gì?

Hàm 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 chuỗi tìm kiếm bằng chuỗi thay thế hoặc mảng thay thế . .

Làm cách nào để xóa chuỗi cụ thể khỏi chuỗi trong PHP?

Bạn có thể sử dụng str_replace() với trim() . Hàm str_replace() thay thế một số ký tự bằng một số ký tự khác trong chuỗi.

Làm cách nào để xóa một từ khỏi chuỗi trong PHP?

Trả lời. Sử dụng hàm str_replace() của PHP Bạn có thể sử dụng hàm str_replace() của PHP để thay thế tất cả các lần xuất hiện của một từ trong một chuỗi.

Làm cách nào để thay thế nhiều 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ế.