Hack Forums
[PHP] Parse inbetween quotes. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Parse inbetween quotes. (/showthread.php?tid=5185451)

Pages: Pages 1 2


Parse inbetween quotes. - DJSanderrr - Feb 27, 2016

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.


* - Subayaku Kokoro - Feb 27, 2016

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



* - DJSanderrr - Feb 27, 2016

(Feb 27, 2016 10:05 AM)KonVex 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!


* - +Haze - Feb 27, 2016

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.


* - Subayaku Kokoro - Feb 27, 2016

(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" 🙃


* - DJSanderrr - Feb 27, 2016

(Feb 27, 2016 11:53 AM)KonVex 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.


* - +Haze - Feb 27, 2016

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.


* - Haxalot - Feb 27, 2016

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.


* - DJSanderrr - Feb 29, 2016

(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!


* - Sakuto - Feb 29, 2016

Strange that nobody suggested the trim function. It's perfect here. trim($yourVariable, '"');


* - DJSanderrr - Feb 29, 2016

(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 :/


* - Sakuto - Feb 29, 2016

I did not run any benchmark, but I highly doubt you're developing something which need high performance, so who care?


* - Haxalot - Feb 29, 2016

(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""



* - Kedox - Feb 29, 2016

This should be the answer you are looking for:
http://stackoverflow.com/a/9735519


* - DJSanderrr - Feb 29, 2016

(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.