php remove quotes from string
Remove quotes from start and end of string in PHP
I have strings like these:
I need to get rid of » (double-quotes) in end and start, if these exist, but if there is this kind of character inside String then it should be left there. Example:
8 Answers 8
The literal answer would be
It will remove all leading and trailing quotes from a string.
If you need to remove strictly the first and the last quote in case they exist, then it could be a regular expression like this
If you need to remove only in case the leading and trailing quote are strictly paired, then use the function from Steve Chambers’ answer
However, if your goal is to read a value from a CSV file, fgetcsv is the only correct option. It will take care of all the edge cases, stripping the value enclosures as well.
I had a similar need and wrote a function that will remove leading and trailing single or double quotes from a string:
This will produce the outputs listed below:
Regex demo (showing what is being matched/captured): https://regex101.com/r/3otW7H/1
trim will remove all instances of the char from the start and end if it matches the pattern you provide, so:
Here’s a way to only remove the first and last char if they match:
As much as this thread should have been killed long ago, I couldn’t help but respond with what I would call the simplest answer of all. I noticed this thread re-emerging on the 17th so I don’t feel quite as bad about this. 🙂
Using samples as provided by Steve Chambers;
This only ever removes the first and last quote, it doesn’t repeat to remove extra content and doesn’t care about matching ends.
This uses \1 to match the same type quote that matched at the beginning. the u modifier, makes it UTF8 capable (okay, not fully multibyte supporting)
Removing single-quote from a string in php
I have an HTML form that a user can input text into a title field, I then have php creating an HTML file called title.html
My problem is that users can input spaces and apostrophes into the title field that can’t be used in the html file name. I replaced the spaces with underscores by using:
However, I can’t seem to remove single-quotes? I have tried using:
7 Answers 7
Using your current str_replace method:
While it’s hard to see, the first argument is a double quote followed by a single quote followed by a double quote. The second argument is two double quotes with nothing in between.
With str_replace, you could even have an array of strings you want to remove entirely:
You can substitute in HTML entitiy:
You could also be more restrictive in removing disallowed characters. The following regex would remove all characters that are not letters, digits or underscores:
You might want to do this to ensure maximum compatibility for filenames across different operating systems.
Try this one. You can strip just ‘ and » with:
I used this function htmlspecialchars for alt attributes in images
Not the answer you’re looking for? Browse other questions tagged php 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.
Remove quotes from string inside of array
I have a string that looks like this:
I need to get rid of the » that are inside of the [] array so it looks like this:
$string = ‘»excludeIF»:[miniTrack, tubeTrack, boxTrack]’;
I was trying some regex but I kept getting rid of all of the quotes.
3 Answers 3
For this particular example:
What this does is it gets the content inside the square brackets, removes the quotes, then replaces the original content with the cleaned content.
This may run into errors if you have the exact same string outside of square brackets later on, but it should be an easy fix if you understand what I’ve written.
PS. It would also help if you showed us what regexes you were trying, as you were, perhaps, on the right path but just had some misunderstandings.
So yeah I agree with the comment about the XY Problem, but I would still like to try help.
You will now need to find the start and end positions of the string that you want edited. This can be done by the following:
Now you have the correct positions you are able to do a substr() to find the exact string you want edited.
From here you can do a simple str_replace()
It is an excellent idea to look at the PHP docs if you struggle to understand any of the above functions. I truly believe that you are only as good at coding as your knowledge of the language is. So please have a read of the following documents:
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
Remove Quotes and Commas from a String in MySQL
I’m importing some data from a CSV file, and numbers that are larger than 1000 get turned into 1,100 etc.
What’s a good way to remove both the quotes and the comma from this so I can put it into an int field?
Edit:
The data is actually already in a MySQL table, so I need to be able to this using SQL. Sorry for the mixup.
8 Answers 8
My guess here is that because the data was able to import that the field is actually a varchar or some character field, because importing to a numeric field might have failed. Here was a test case I ran purely a MySQL, SQL solution.
The table is just a single column (alpha) that is a varchar.
So in the end the statement I used was:
I looked at the MySQL Documentation and it didn’t look like I could do the regular expressions find and replace. Although you could, like Eldila, use a regular expression for a find and then an alternative solution for replace.
Also be careful with s/»(\d+),(\d+)»/$1$2/ because what if the number has more then just a single comma, for instance «1,000,000» you’re going to want to do a global replace (in perl that is s///g ). But even with a global replace the replacement starts where you last left off (unless perl is different), and would miss the every other comma separated group. A possible solution would be to make the first (\d+) optional like so s/(\d+)?,(\d+)/$1$2/g and in this case I would need a second find and replace to strip the quotes.
Here are some ruby examples of the regular expressions acting on just the string «1,000,000», notice there are NOT double quote inside the string, this is just a string of the number itself.
Here is a good case for regular expressions. You can run a find and replace on the data either before you import (easier) or later on if the SQL import accepted those characters (not nearly as easy). But in either case, you have any number of methods to do a find and replace, be it editors, scripting languages, GUI programs, etc. Remember that you’re going to want to find and replace all of the bad characters.
A typical regular expression to find the comma and quotes (assuming just double quotes) is: (Blacklist)
Or, if you find something might change in the future, this regular expression, matches anything except a number or decimal point. (Whitelist)
What has been discussed by the people above is that we don’t know all of the data in your CSV file. It sounds like you want to remove the commas and quotes from all of the numbers in the CSV file. But because we don’t know what else is in the CSV file we want to make sure that we don’t corrupt other data. Just blindly doing a find/replace could affect other portions of the file.
You could use this perl command.
You may need to play around with it a bit, but it should do the trick.
Actually nlucaroni, your case isn’t quite right. Your example doesn’t include double-quotes, so
won’t match my regex. It requires the format «XXX,XXX». I can’t think of an example of when it will match incorrectly.
All the following example won’t include the deliminator in the regex:
Please let me know if you can think of a counter-example.
The solution to the changed question is basically the same.
You will have to run select query with the regex where clause.
Foreach of these rows, you want to do the following regex substitution s/»(\d+),(\d+)»/$1$2/ and then update the field with the new value.
Please Joseph Pecoraro seriously and have a backup before doing mass changes to any files or databases. Because whenever you do regex, you can seriously mess up data if there are cases that you have missed.
My command does remove all ‘,’ and ‘»‘.
In order to convert the sting «1,000» more strictly, you will need the following command.
Daniel’s and Eldila’s answer have one problem: They remove all quotes and commas in the whole file.
What I usually do when I have to do something like this is to first replace all separating quotes and (usually) semicolons by tabs.
Since I know in which column my affected values will be I then do another search and replace:
. given the value with the comma is in the third column.
You need to start with an «^» to make sure that it starts at the beginning of a line. Then you repeat (6+)\t as often as there are columns that you just want to leave as they are.
(3+),(6+) searches for values where there is a number, then a comma and then another number.
In the replace string we use \1 and \2 to just keep the values from the edited line, separating them with \t (tab). Then we put \3\4 (no tab between) to put the two components of the number without the comma right after each other. All values after that will be left alone.
I usually do that in an ordinary text editor (EditPlus) that supports RegExp, but the same regexps can be used in any programming language.