How to remove the leading character from a string?

PhpStringTrim

Php Problem Overview


I have a input string like:

$str = ':this is a applepie :) ';

How can I remove the first occurring : with PHP?

Desired output: this is a applepie :)

Php Solutions


Solution 1 - Php

The substr() function will probably help you here:

 $str = substr($str, 1);

Strings are indexed starting from 0, and this functions second parameter takes the cutstart. So make that 1, and the first char is gone.

Solution 2 - Php

To remove every : from the beginning of a string, you can use ltrim:

$str = '::f:o:';
$str = ltrim($str, ':');
var_dump($str); //=> 'f:o:'

Solution 3 - Php

Use substr:

$str = substr($str, 1); // this is a applepie :)

Solution 4 - Php

Exec time for the 3 answers :

Remove the first letter by replacing the case

$str = "hello";
$str[0] = "";
// $str[0] = false;
// $str[0] = null;
// replaced by �, but ok for echo

Exec time for 1.000.000 tests : 0.39602184295654 sec


Remove the first letter with substr()

$str = "hello";
$str = substr($str, 1);

Exec time for 1.000.000 tests : 5.153294801712 sec


Remove the first letter with ltrim()

$str = "hello";
$str= ltrim ($str,'h');

Exec time for 1.000.000 tests : 5.2393000125885 sec


Remove the first letter with preg_replace()

$str = "hello";
$str = preg_replace('/^./', '', $str);

Exec time for 1.000.000 tests : 6.8543920516968 sec

Solution 5 - Php

The accepted answer:

$str = ltrim($str, ':');

works but will remove multiple : when there are more than one at the start.

$str = substr($str, 1);

will remove any character from the start.

However,

if ($str[0] === ':')
    $str = substr($str, 1);

works perfectly.

Solution 6 - Php

$str = substr($str, 1);

See PHP manual example 3

echo substr('abcdef', 1);     // bcdef

Note:

unset($str[0]) 

will not work as you cannot unset part of a string:-

Fatal error: Cannot unset string offsets

Solution 7 - Php

Update

After further tests, I don't recommend using this any more. It caused a problem for me when using the updated string in a MySQL query, and changing to substr fixed the problem. I thought about deleting this answer, but comments suggest it is quicker somehow so someone might have a use for it. You may find trimming the updated string resolves string length issues.


Sometimes you don't need a function:

$str[0] = '';

For example:

$str = 'AHello';
$str[0] = '';
echo $str; // 'Hello'

This method modifies the existing string rather than creating another.

Solution 8 - Php

Trims occurrences of every word in an array from the beginning and end of a string + whitespace and optionally extra single characters as per normal trim()

<?php
function trim_words($what, $words, $char_list = '') {
    if(!is_array($words)) return false;
    $char_list .= " \t\n\r\0\x0B"; // default trim chars
    $pattern = "(".implode("|", array_map('preg_quote', $words)).")\b";
    $str = trim(preg_replace('~'.$pattern.'$~i', '', preg_replace('~^'.$pattern.'~i', '', trim($what, $char_list))), $char_list);
    return $str;
}

// for example:
$trim_list = array('AND', 'OR');

$what = ' OR x = 1 AND b = 2 AND ';
print_r(trim_words($what, $trim_list)); // => "x = 1 AND b = 2"

$what = ' ORDER BY x DESC, b ASC, ';
print_r(trim_words($what, $trim_list, ',')); // => "ORDER BY x DESC, b ASC"
?>

Solution 9 - Php

use mb_substr function

    mb_substr("我abc", 1);

Solution 10 - Php

The code works well for me.

$str = substr($str ,-(strlen($str)-1));

Maybe, contribute with answers too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionyuli chikaView Question on Stackoverflow
Solution 1 - PhpmarioView Answer on Stackoverflow
Solution 2 - PhpHaim EvgiView Answer on Stackoverflow
Solution 3 - PhpalexnView Answer on Stackoverflow
Solution 4 - PhpHayennView Answer on Stackoverflow
Solution 5 - PhpDan BrayView Answer on Stackoverflow
Solution 6 - PhpGammerzView Answer on Stackoverflow
Solution 7 - Phprybo111View Answer on Stackoverflow
Solution 8 - PhpBenView Answer on Stackoverflow
Solution 9 - Php风声猎猎View Answer on Stackoverflow
Solution 10 - PhpcalraidenView Answer on Stackoverflow