php string remove char from string
Remove the last character from a string [duplicate]
What is fastest way to remove the last character from a string?
I have a string like
I would like to remove the last ‘,’ and get the remaining string back:
What is the fastest way to do this?
4 Answers 4
First, I try without a space, rtrim($arraynama, «,»); and get an error result.
Then I add a space and get a good result:
An alternative to substr is the following, as a function:
Is it the fastest? I don’t know, but I’m willing to bet these alternatives are all so fast that it just doesn’t matter.
See substr in the PHP documentation. It returns part of a string.
Not the answer you’re looking for? Browse other questions tagged php or ask your own question.
Linked
Related
Hot Network Questions
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Remove newline character from a string using PHP regex
How can I remove a new line character from a string using PHP?
9 Answers 9
To remove several new lines it’s recommended to use a regular expression:
Because some line breaks remains as it is from textarea input.
Something a bit more functional (easy to use anywhere):
stripcslashes should suffice (removes \r\n etc.)
Try this out. It’s working for me.
First remove n from the string (use double slash before n ).
Then remove r from string like n
Let’s see a performance test!
Things have changed since I last answered this question, so here’s a little test I created. I compared the four most promising methods, preg_replace vs. strtr vs. str_replace, and strtr goes twice because it has a single character and an array-to-array mode.
You can run the test here:
Results
(Note that it’s a realtime test and server loads may change, so you’ll probably get different figures.)
The preg_replace solution is noticeably slower, but that’s okay. They do a different job and PHP has no prepared regex, so it’s parsing the expression every single time. It’s simply not fair to expect them to win.
The last one is a dirty trick: it replaces characters with characters, that is, newlines with spaces. It’s even faster, and it makes sense because when you get rid of line breaks, you probably don’t want to concatenate the word at the end of one line with the first word of the next. So it’s not exactly what the OP described, but it’s clearly the fastest. With long strings and many replacements, the difference will grow because character substitutions are linear by nature.
Verdict: str_replace wins in general
Removing characters from a PHP String
I’m accepting a string from a feed for display on the screen that may or may not include some rubbish I want to filter out. I don’t want to filter normal symbols at all.
The values I want to remove look like this: �
It is only this that I want removed. Relevant technology is PHP.
8 Answers 8
This is an encoding problem; you shouldn’t try to clean that bogus characters but understand why you’re receiving them scrambled.
Try to get your data as Unicode, or to make a agreement with your feed provider to you both use the same encoding.
Thanks for the responses, guys. Unfortunately, those submitted had the following problems:
wrong for obvious reasons:
which also uses the deprecated ereg form of regex also didn’t work when I converted to preg because the range was simply too large for the regex to handle. Also, there are holes in that range that would allow rubbish to seep through.
This is an encoding problem; you shouldn’t try to clean that bogus characters but understand why you’re receiving them scrambled.
while valid, is no good because I don’t have any control over how the data I receive is encoded. It comes from an external source. Sometimes there’s garbage in there and sometimes there is not.
So, the solution I came up with was relatively dirty, but in the absence of something more robust I’m just accepting all standard letters, numbers and symbols and discarding the rest.
This does seem to work for now. The solution is as follows:
If anyone has any better ideas I’m still keen to hear them. Cheers.
How to remove the leading character from a string?
I have a input string like:
How can I remove the first occurring : with PHP?
Desired output: this is a applepie 🙂
10 Answers 10
The substr() function will probably help you here:
Strings are indexed starting from 0, and this functions second parameter takes the cutstart. So make that 1, and the first char is gone.
To remove every : from the beginning of a string, you can use ltrim:
Exec time for the 3 answers :
Remove the first letter by replacing the case
Exec time for 1.000.000 tests : 0.39602184295654 sec
Remove the first letter with substr()
Exec time for 1.000.000 tests : 5.153294801712 sec
Remove the first letter with ltrim()
Exec time for 1.000.000 tests : 5.2393000125885 sec
Remove the first letter with preg_replace()
Exec time for 1.000.000 tests : 6.8543920516968 sec
The accepted answer:
works but will remove multiple : when there are more than one at the start.
will remove any character from the start.
will not work as you cannot unset part of a string:-
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:
This method modifies the existing string rather than creating another.
Remove last character from a string
Tried to remove it with
9 Answers 9
I think that you’re missing to assign the variable back after the trim:
Demo
try this substr() or mb_substr()
This function should replace each occurence of a comma with a space.
Use implode instead:
This will put a comma at the start of each appended string except for the first one. The is no longer a trailing comma to deal with so no need to try trimming it off.
// Try to use chop() function in php. It helps in removing last character from a string
Output : ‘USA’,’UK’,’India’
Not the answer you’re looking for? Browse other questions tagged php jquery or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.