304

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 :)

TylerH
  • 20,799
  • 66
  • 75
  • 101
yuli chika
  • 9,053
  • 20
  • 75
  • 122
  • 1
    possible duplicate of [PHP Subtract First Character of String](http://stackoverflow.com/questions/3592000/php-subtract-first-character-of-string) – Gordon Jan 09 '11 at 11:48
  • 1
    possible duplicate of [Remove first 4 characters of a string php](http://stackoverflow.com/questions/4286423/remove-first-4-characters-of-a-string-php) – Gordon Jan 09 '11 at 11:48

10 Answers10

623

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.

Wistar
  • 3,770
  • 4
  • 45
  • 70
mario
  • 144,265
  • 20
  • 237
  • 291
  • 38
    Be aware of unicode. If you're dealing with an arbitrary string (e.g. "Ål <- danish for eel"), you should use mb_substr and specify the encoding. – Thomas Jensen Jun 17 '12 at 11:22
  • this is not a correct implementation, it not working with single character string "a". if you try single character string, substr return a boolean value – anru Oct 26 '15 at 02:20
  • 2
    @anru The manual states if the string length is equal to the start parameter, an empty string will be returned. Prior to version 7, `false` was returned. So you would need to check if they're equal if that is not the behaviour you want. – rybo111 Feb 15 '16 at 16:18
356

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

$str = '::f:o:';
$str = ltrim($str, ':');
var_dump($str); //=> 'f:o:'
tleb
  • 4,395
  • 3
  • 25
  • 33
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • 122
    Note that lrtim will remove all `:::::`. Sometimes this is not desired behavior. – CoR Nov 20 '13 at 14:02
  • 5
    note that this is only intended for single characters. ltrim will trim all of the characters in the provided string: `ltrim('prefixprefix_postfix', 'prefix')` results in `'_postfix';` – Cody Django Apr 05 '17 at 22:19
  • 2
    "How to remove the first character of string in PHP" was the question and this comment doesn't actually answer that. – Luke Sep 08 '20 at 13:00
  • Its replacing all when searching for "0" zero. – Krishan Kumar Apr 14 '22 at 08:52
98

Use substr:

$str = substr($str, 1); // this is a applepie :)
alexn
  • 57,867
  • 14
  • 111
  • 145
71

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

Hayenn
  • 818
  • 6
  • 7
  • Thanks. See my update, though. It caused a problem for me when using the updated string in an SQL query. – rybo111 Mar 19 '14 at 23:16
  • 6
    I just tried the `$str[0] = '';` solution and it *didn't* work. well it does however if you then plan on using the variable for example to compare `>` or `<` it won't work. It still counts ` ` as +` for example `$str = 'hello'; $str[0] = ''; var_dump($str); // string(5) 'ello'` – Ian Aug 07 '14 at 15:44
  • @Ian: I came across the same issue while fetching records from an API using a keyword, tried `var_dump($keyword)` which was showing the previous character length.. then I tried trimming the keyword and then it worked fine `var_dump(trim($keyword))`.. Hope this helps someone.. :) – Qarib Haider Mar 08 '15 at 07:45
  • 3
    This does not work. The "removed" position is replaced with null byte, so you get "\0hello" instead of "hello". – Josef Kufner Apr 24 '15 at 17:18
  • 1
    I get "Warning: Cannot assign an empty string to a string offset" for $str[0] = ""; – Francisco Luz Apr 30 '19 at 05:05
  • Hi @Hayenn Could you show me how you do the speed tests for PHP functionalities? – Lenin Zapata Aug 03 '19 at 23:22
19

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.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
9

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.

rybo111
  • 12,240
  • 4
  • 61
  • 70
8
$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
Gammerz
  • 81
  • 1
  • 3
1

use mb_substr function

    mb_substr("我abc", 1);
风声猎猎
  • 1,065
  • 1
  • 7
  • 9
0

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"
?>
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ben
  • 4,392
  • 3
  • 23
  • 20
  • This answer seems to be completely ignoring the OP's question requirements and sample string. This is, at most, the correct answer to a different question. – mickmackusa Mar 18 '21 at 22:26
-1

The code works well for me.

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

Maybe, contribute with answers too.

calraiden
  • 1,686
  • 1
  • 27
  • 37
  • 1
    this one is not working with single character string: like "a" – anru Oct 26 '15 at 02:14
  • You're right =) So, believe that a possible solution would be to test before it only contains one character. For example: $str = strlen($str) > 1 ? substr($str ,-(strlen($str)-1)) : $str; Or $str = strlen($str) > 1 ? substr($str ,-(strlen($str)-1)) : ''; because the idea is to remove the last character, if you only have one ... :P – calraiden Nov 12 '15 at 12:24