Replace first character in string php
I'm trying to replace only the first character of a string, but it isn't working, it replaces all the "0" on the string, here's my code: Show
I want to replace the first character only if its a 0, example:
I don't want to replace all the time, example:
I also tried this way, but it didn't work the way i want, because it's replacing also the second character if it's 0, like 10 becomes 1. (PHP 4, PHP 5, PHP 7, PHP 8) substr_replace — Replace text within a portion of a string Descriptionsubstr_replace( Parametersstring The input string. An array of
strings can be provided, in which case the replacements will occur on each string in turn. In this case, the replace The replacement string. offset If If length If given and is positive, it represents the length of the portion of Return Values The result string is returned. If Changelog
ExamplesExample #1 Simple substr_replace() examples
Example #2 Using substr_replace() to replace multiple strings at once
The above example will output: A: YYY; B: YYY; C: YYY A: AAA; B: BBB; C: CCC A: AAAXX; B: BBBX; C: CCC Notes
See Also
elloromtz at gmail dot com ¶ 12 years ago
billg AT microsoft.com ¶ 13 years ago
$start < 0) eblejr AT phrebh DOT com ¶ 14 years ago
juichenieder-phnet at yahoo dot co dot uk ¶ 13 years ago
ivijan dot stefan at gmail dot com ¶ 8 years ago
danieldoorduin at hotmail dot com ¶ 17 years ago
Hayley Watson ¶ 4 years ago
Thijs Wijnmaalen (thijs[at]nllinux.nl) ¶ 18 years ago
strlen($text) > $maxChars) { William Barry ¶ 14 years ago
// Find the first closing ul kalim dot fleet at gmail dot com ¶ 12 years ago
= 'abcdefghijklmnopqrstuvwxyz0123456789z.jpg'; mrbrown8 at juno dot com ¶ 21 years ago
hermes at andycostell dot com ¶ 17 years ago
den dot gierling at web dot de ¶ 12 years ago
Guru Evi ¶ 17 years ago
klaas at group94 dot com ¶ 20 years ago
alishahnovin at hotmail dot com ¶ 15 years ago
shaman_master at list dot ru ¶ 2 years ago
bkline at rksystems dot com ¶ 3 years ago
meg dot phillips91 at gmail dot com ¶ 2 years ago
yeyijelud at amadamus dot com ¶ 3 years ago
NiX0n at fragfest dot cx ¶ 13 years ago
spcl dot delivery at gmail dot com ¶ 14 years ago
jaimthorn at yahoo dot com ¶ 14 years ago
chuayw2000 at hotmail dot com ¶ 16 years ago
david at ethinkn dot com ¶ 19 years ago
truncate ('akfhslakdhglksjdgh', 10, '...'); nospam at nospam dot com ¶ 6 years ago
strShorten('123456789', 6, ''); // outputs 123789 dmron ¶ 18 years ago
Anonymous ¶ 20 years ago
blessador at gmail dot com ¶ 9 years ago
at the end of substr_replace ($price, ',', -3, 0)" in the above code to avoid error.
geniusdex ( at ) brz ( dot ) nu ¶ 18 years ago
tony at outshine dot com ¶ 18 years ago
ntoniazzi at sqli dot com ¶ 14 years ago
thomasNOSPAM at sportentranceNOSPAM dot com ¶ 19 years ago
olav at schettler dot net ¶ 6 years ago
admiral at nuclearpixel dot com ¶ 12 years ago
admiralsTruncate($my_string, 6); // outputs: cfcd20echo ' How do you replace the first character in a string?To replace the first character in a string:
Assign the character at index 0 to a variable. Use the String. replace() method to replace the character in the string. The replace method will return a new string with the first character replaced.
How can I replace part of a string in PHP?The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.
How do you remove portion of a string before a certain character in PHP?You can use strstr to do this. Show activity on this post. The explode is in fact a better answer, as the question was about removing the text before the string.
How can I replace special characters in a string in PHP?The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.
|