DO NOT DEAL WITH THIS MEMBER
Hello,
I was wondering what the fastest method is to parse the text inbetween quotes.
So that
Quote:"abc cba"
will become
Quote:abc cba
I've read many methods with regex, but I need to have about 500 of these requests a second, so was wondering if there's anything faster.
Thanks in advance.
what exactly are you trying to do?
do you only get 1 string with quotes ? or a whole text or whatever?
why just dont use
PHP Code
<?php str_replace('"','',$string);
IF you just get a string
or if you really need regext make it like this
PHP Code
<?php preg_match('/".*?"/', $x, $matches);
print_r($matches);
DO NOT DEAL WITH THIS MEMBER
(Feb 27, 2016 10:05 AM)Subayaku Kokoro Wrote: why just dont use
PHP Code
<?php str_replace('"','',$string);
Wow I was so stupid.. damn I really didn't think of that..
Thanks!
but if you have,
"my name" is "haze"
string replace won't work.
depending on the format you expect the data in, string replace might not be accurate.
(Feb 27, 2016 10:23 AM)+Haze Wrote: but if you have,
"my name" is "haze"
string replace won't work.
depending on the format you expect the data in, string replace might not be accurate.
yeah thats why i asked what he is trying to do .. but i think he dont need any "complicated" solution for his "problem" 🙃
DO NOT DEAL WITH THIS MEMBER
(Feb 27, 2016 11:53 AM)Subayaku Kokoro Wrote: yeah thats why i asked what he is trying to do .. but i think he dont need any "complicated" solution for his "problem" 🙃
Actually, I'm currently running against that problem..
I actually now need to only replace " to null if it's eighter at the beginning or the end of the string.
How would I do this WITHOUT regex, and first check if the character IS there.
Feb 27, 2016 12:03 PM
(This post was last modified: Feb 27, 2016 12:06 PM by +Haze.)
Just use regex, it's probably your best bet. Here's the expression.
"(.*?)"
I mean you can also use like str pos, but like that's just extra long code. Regex would suffice.
Feb 27, 2016 12:09 PM
(This post was last modified: Feb 27, 2016 12:10 PM by Haxalot.)
Well, one way would be to set the first and last offset of the string to empty strings:
PHP Code
<?php $a = '"abc"'; $a[0] = ''; $a[strlen($a) - 1] = ''; var_dump($a); // string(5) "abc"
Unfortunately though, this will set the empty string offsets to null byte characters (\0), hence why the string's length remains 5 characters long (I believe the bug has been fixed in PHP's master branch). Since the null byte characters may have adverse affects on your script, using substr would probably be the better approach:
PHP Code
<?php $a = '"abc"'; var_dump(substr($a, 1, -1)); // string(3) "abc"
Obviously if the leading and trailing quote is optional, then you'll want to place in some checks.
PHP internals developer
DO NOT DEAL WITH THIS MEMBER
(Feb 27, 2016 12:09 PM)Haxalot Wrote: Well, one way would be to set the first and last offset of the string to empty strings:
PHP Code
<?php $a = '"abc"'; $a[0] = ''; $a[strlen($a) - 1] = ''; var_dump($a); // string(5) "abc"
Unfortunately though, this will set the empty string offsets to null byte characters (\0), hence why the string's length remains 5 characters long (I believe the bug has been fixed in PHP's master branch). Since the null byte characters may have adverse affects on your script, using substr would probably be the better approach:
PHP Code
<?php $a = '"abc"'; var_dump(substr($a, 1, -1)); // string(3) "abc"
Obviously if the leading and trailing quote is optional, then you'll want to place in some checks.
Thank you. I combined both your solutions somehow and works like a charm now!
Strange that nobody suggested the trim function. It's perfect here. trim($yourVariable, '"');
DO NOT DEAL WITH THIS MEMBER
(Feb 29, 2016 04:55 AM)Sakuto Wrote: Strange that nobody suggested the trim function. It's perfect here. trim($yourVariable, '"');
Just looked at that and seems awesome! Thank you.
Is it also sincificant faster than the other functions? I can't run a test at this moment :/
I did not run any benchmark, but I highly doubt you're developing something which need high performance, so who care?
(Feb 29, 2016 04:55 AM)Sakuto Wrote: Strange that nobody suggested the trim function. It's perfect here. trim($yourVariable, '"');
I avoided suggesting trim() because we aren't sure where the double quotes may occur in the string. We only know that OP wants to remove the first and last double quote of a string. trim() would therefore screw up the following string:
Code "Quoting Abc: "def""
PHP internals developer
This should be the answer you are looking for:
http://stackoverflow.com/a/9735519
DO NOT DEAL WITH THIS MEMBER
(Feb 29, 2016 09:18 AM)Haxalot Wrote: I avoided suggesting trim() because we aren't sure where the double quotes may occur in the string. We only know that OP wants to remove the first and last double quote of a string. trim() would therefore screw up the following string:
Code "Quoting Abc: "def""
No I actually only want to trim if it occurs at the beginning or the end of the string.
I apologize if I was unclear at the beginning.
|