Hướng dẫn remove string from beginning of string php - xóa chuỗi khỏi đầu chuỗi php

Hình thức đơn giản, không có regex:

$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';

if [substr[$str, 0, strlen[$prefix]] == $prefix] {
    $str = substr[$str, strlen[$prefix]];
} 

Mất: 0,0369 ms [0,000,036,954 giây]0.0369 ms [0.000,036,954 seconds]

Và với:

$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
$str = preg_replace['/^' . preg_quote[$prefix, '/'] . '/', '', $str];

Mất: 0,1749 ms [0,000,174.999 giây] lần chạy đầu tiên [biên dịch] và 0,0510 ms [0,000.051.021 giây] sau.0.1749 ms [0.000,174,999 seconds] the 1st run [compiling], and 0.0510 ms [0.000,051,021 seconds] after.

Biểu đồ trên máy chủ của tôi, rõ ràng.

Blessador tại Gmail Dot Com ¶

9 năm trước

Tony tại Outshine Dot Com ¶

  • GeniusDex [at] brz [dot] nu ¶
  • ntoniazzi tại sqli dot com ¶
  • Blessador tại Gmail Dot Com ¶

    9 năm trước

    Tony tại Outshine Dot Com ¶

    GeniusDex [at] brz [dot] nu ¶

    Examples:

    Input : Geeksforgeeks
    Output : eeksforgeeks
    
    Input :, Hello geek!
    Output : Hello geek!
    

    ntoniazzi tại sqli dot com ¶{IDE} first, before moving on to the solution.

    Thomasnospam tại Sportentrancenospam dot com ¶
    In PHP to remove characters from beginning we can use ltrim but in that we have to define what we want to remove from a string i.e. removing characters are to be known.
    Example:

    20 năm trước

    Olav tại Schettler Dot Net

    Đô đốc tại nuclearpixel dot com ¶

    13 năm trước

    Input : Geeksforgeeks
    Output : eeksforgeeks
    
    Input :, Hello geek!
    Output : Hello geek!
    
    2

    Làm cách nào để xóa một phần của chuỗi trong PHP?substr[]. Here we can use it by two parameters one is the string and the other is the index. substr[] return string from the second parameter index to the end of the string.

    Bài Viết Liên Quan

    Chủ Đề