Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the leading character from a string?

Tags:

string

php

trim

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

like image 665
yuli chika Avatar asked Jan 09 '11 09:01

yuli chika


People also ask

How do I remove the first character of a string in Java?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.

How do you remove the first character of a string in Python?

Remove the First Character From the String in Python Using the str. lstrip() Method. The str. lstrip() method takes one or more characters as input, removes them from the start of the string, and returns a new string with removed characters.

How do I remove the first character?

Remove first character in Excel To delete the first character from a string, you can use either the REPLACE function or a combination of RIGHT and LEN functions. Here, we simply take 1 character from the first position and replace it with an empty string ("").


2 Answers

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.

like image 180
mario Avatar answered Oct 27 '22 19:10

mario


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

$str = '::f:o:'; $str = ltrim($str, ':'); var_dump($str); //=> 'f:o:' 
like image 40
Haim Evgi Avatar answered Oct 27 '22 18:10

Haim Evgi