php to extract a string from double quote

Multi tool use
I have a string:
This is a text, "Your Balance left $0.10", End 0
How can I extract the string in between the double quotes and have only the text (without the double quotes):
Your Balance left $0.10
I have tried preg_match_all()
but with no luck.
php string preg-match preg-match-all double-quotes
add a comment |
I have a string:
This is a text, "Your Balance left $0.10", End 0
How can I extract the string in between the double quotes and have only the text (without the double quotes):
Your Balance left $0.10
I have tried preg_match_all()
but with no luck.
php string preg-match preg-match-all double-quotes
You might finds($str)->between('"', '"')
helpful, as found in this standalone library.
– caw
Jul 27 '16 at 2:34
add a comment |
I have a string:
This is a text, "Your Balance left $0.10", End 0
How can I extract the string in between the double quotes and have only the text (without the double quotes):
Your Balance left $0.10
I have tried preg_match_all()
but with no luck.
php string preg-match preg-match-all double-quotes
I have a string:
This is a text, "Your Balance left $0.10", End 0
How can I extract the string in between the double quotes and have only the text (without the double quotes):
Your Balance left $0.10
I have tried preg_match_all()
but with no luck.
php string preg-match preg-match-all double-quotes
php string preg-match preg-match-all double-quotes
edited Jun 18 '13 at 0:58


Just Plain High
767621
767621
asked Jun 19 '09 at 9:17
conandorconandor
1,37551833
1,37551833
You might finds($str)->between('"', '"')
helpful, as found in this standalone library.
– caw
Jul 27 '16 at 2:34
add a comment |
You might finds($str)->between('"', '"')
helpful, as found in this standalone library.
– caw
Jul 27 '16 at 2:34
You might find
s($str)->between('"', '"')
helpful, as found in this standalone library.– caw
Jul 27 '16 at 2:34
You might find
s($str)->between('"', '"')
helpful, as found in this standalone library.– caw
Jul 27 '16 at 2:34
add a comment |
6 Answers
6
active
oldest
votes
As long as the format stays the same you can do this using a regular expression. "([^"]+)"
will match the pattern
- Double-quote
- At least one non-double-quote
- Double-quote
The brackets around the [^"]+
means that that portion will be returned as a separate group.
<?php
$str = 'This is a text, "Your Balance left $0.10", End 0';
//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}
//output: Your Balance left $0.10
+1 simple and sweet
– diEcho
Apr 19 '13 at 5:53
add a comment |
For everyone hunting for a full featured string parser, try this:
(?:(?:"(?:\"|[^"])+")|(?:'(?:\'|[^'])+'));
Use in preg_match:
$haystack = "something else before 'Lars' Teststring in quotes' something else after";
preg_match("/(?:(?:"(?:\\"|[^"])+")|(?:'(?:\'|[^'])+'))/is",$haystack,$match);
Returns:
Array
(
[0] => 'Lars' Teststring in quotes'
)
This works with single and double quoted string fragments.
3
Works but is there a way to exclude the quotes itself from the returned string?
– 3zzy
Jul 25 '15 at 13:45
add a comment |
Try this :
preg_match_all('`"([^"]*)"`', $string, $results);
You should get all your extracted strings in $results[1].
+1 For providing a solution with preg_match_all
– demongolem
Aug 14 '12 at 15:39
1
This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.
– Warre Buysse
Apr 26 '14 at 2:31
add a comment |
Unlike other answers, this supports escapes, e.g. "string with " quote in it"
.
$content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));
add a comment |
The regular expression '"([^\"]+)"'
will match anything between two double quotes.
$string = '"Your Balance left $0.10", End 0';
preg_match('"([^\"]+)"', $string, $result);
echo $result[0];
add a comment |
Just use str_replace and escape the quote:
str_replace(""","",$yourString);
Edit:
Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1017051%2fphp-to-extract-a-string-from-double-quote%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
As long as the format stays the same you can do this using a regular expression. "([^"]+)"
will match the pattern
- Double-quote
- At least one non-double-quote
- Double-quote
The brackets around the [^"]+
means that that portion will be returned as a separate group.
<?php
$str = 'This is a text, "Your Balance left $0.10", End 0';
//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}
//output: Your Balance left $0.10
+1 simple and sweet
– diEcho
Apr 19 '13 at 5:53
add a comment |
As long as the format stays the same you can do this using a regular expression. "([^"]+)"
will match the pattern
- Double-quote
- At least one non-double-quote
- Double-quote
The brackets around the [^"]+
means that that portion will be returned as a separate group.
<?php
$str = 'This is a text, "Your Balance left $0.10", End 0';
//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}
//output: Your Balance left $0.10
+1 simple and sweet
– diEcho
Apr 19 '13 at 5:53
add a comment |
As long as the format stays the same you can do this using a regular expression. "([^"]+)"
will match the pattern
- Double-quote
- At least one non-double-quote
- Double-quote
The brackets around the [^"]+
means that that portion will be returned as a separate group.
<?php
$str = 'This is a text, "Your Balance left $0.10", End 0';
//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}
//output: Your Balance left $0.10
As long as the format stays the same you can do this using a regular expression. "([^"]+)"
will match the pattern
- Double-quote
- At least one non-double-quote
- Double-quote
The brackets around the [^"]+
means that that portion will be returned as a separate group.
<?php
$str = 'This is a text, "Your Balance left $0.10", End 0';
//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}
//output: Your Balance left $0.10
edited Mar 20 '17 at 14:52
answered Jun 19 '09 at 9:28
Tom HaighTom Haigh
48.6k18102135
48.6k18102135
+1 simple and sweet
– diEcho
Apr 19 '13 at 5:53
add a comment |
+1 simple and sweet
– diEcho
Apr 19 '13 at 5:53
+1 simple and sweet
– diEcho
Apr 19 '13 at 5:53
+1 simple and sweet
– diEcho
Apr 19 '13 at 5:53
add a comment |
For everyone hunting for a full featured string parser, try this:
(?:(?:"(?:\"|[^"])+")|(?:'(?:\'|[^'])+'));
Use in preg_match:
$haystack = "something else before 'Lars' Teststring in quotes' something else after";
preg_match("/(?:(?:"(?:\\"|[^"])+")|(?:'(?:\'|[^'])+'))/is",$haystack,$match);
Returns:
Array
(
[0] => 'Lars' Teststring in quotes'
)
This works with single and double quoted string fragments.
3
Works but is there a way to exclude the quotes itself from the returned string?
– 3zzy
Jul 25 '15 at 13:45
add a comment |
For everyone hunting for a full featured string parser, try this:
(?:(?:"(?:\"|[^"])+")|(?:'(?:\'|[^'])+'));
Use in preg_match:
$haystack = "something else before 'Lars' Teststring in quotes' something else after";
preg_match("/(?:(?:"(?:\\"|[^"])+")|(?:'(?:\'|[^'])+'))/is",$haystack,$match);
Returns:
Array
(
[0] => 'Lars' Teststring in quotes'
)
This works with single and double quoted string fragments.
3
Works but is there a way to exclude the quotes itself from the returned string?
– 3zzy
Jul 25 '15 at 13:45
add a comment |
For everyone hunting for a full featured string parser, try this:
(?:(?:"(?:\"|[^"])+")|(?:'(?:\'|[^'])+'));
Use in preg_match:
$haystack = "something else before 'Lars' Teststring in quotes' something else after";
preg_match("/(?:(?:"(?:\\"|[^"])+")|(?:'(?:\'|[^'])+'))/is",$haystack,$match);
Returns:
Array
(
[0] => 'Lars' Teststring in quotes'
)
This works with single and double quoted string fragments.
For everyone hunting for a full featured string parser, try this:
(?:(?:"(?:\"|[^"])+")|(?:'(?:\'|[^'])+'));
Use in preg_match:
$haystack = "something else before 'Lars' Teststring in quotes' something else after";
preg_match("/(?:(?:"(?:\\"|[^"])+")|(?:'(?:\'|[^'])+'))/is",$haystack,$match);
Returns:
Array
(
[0] => 'Lars' Teststring in quotes'
)
This works with single and double quoted string fragments.
edited Jun 18 '13 at 0:45


Just Plain High
767621
767621
answered Aug 19 '11 at 14:50
user426486user426486
15114
15114
3
Works but is there a way to exclude the quotes itself from the returned string?
– 3zzy
Jul 25 '15 at 13:45
add a comment |
3
Works but is there a way to exclude the quotes itself from the returned string?
– 3zzy
Jul 25 '15 at 13:45
3
3
Works but is there a way to exclude the quotes itself from the returned string?
– 3zzy
Jul 25 '15 at 13:45
Works but is there a way to exclude the quotes itself from the returned string?
– 3zzy
Jul 25 '15 at 13:45
add a comment |
Try this :
preg_match_all('`"([^"]*)"`', $string, $results);
You should get all your extracted strings in $results[1].
+1 For providing a solution with preg_match_all
– demongolem
Aug 14 '12 at 15:39
1
This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.
– Warre Buysse
Apr 26 '14 at 2:31
add a comment |
Try this :
preg_match_all('`"([^"]*)"`', $string, $results);
You should get all your extracted strings in $results[1].
+1 For providing a solution with preg_match_all
– demongolem
Aug 14 '12 at 15:39
1
This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.
– Warre Buysse
Apr 26 '14 at 2:31
add a comment |
Try this :
preg_match_all('`"([^"]*)"`', $string, $results);
You should get all your extracted strings in $results[1].
Try this :
preg_match_all('`"([^"]*)"`', $string, $results);
You should get all your extracted strings in $results[1].
answered Jun 19 '09 at 9:28
Arkh
+1 For providing a solution with preg_match_all
– demongolem
Aug 14 '12 at 15:39
1
This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.
– Warre Buysse
Apr 26 '14 at 2:31
add a comment |
+1 For providing a solution with preg_match_all
– demongolem
Aug 14 '12 at 15:39
1
This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.
– Warre Buysse
Apr 26 '14 at 2:31
+1 For providing a solution with preg_match_all
– demongolem
Aug 14 '12 at 15:39
+1 For providing a solution with preg_match_all
– demongolem
Aug 14 '12 at 15:39
1
1
This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.
– Warre Buysse
Apr 26 '14 at 2:31
This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.
– Warre Buysse
Apr 26 '14 at 2:31
add a comment |
Unlike other answers, this supports escapes, e.g. "string with " quote in it"
.
$content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));
add a comment |
Unlike other answers, this supports escapes, e.g. "string with " quote in it"
.
$content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));
add a comment |
Unlike other answers, this supports escapes, e.g. "string with " quote in it"
.
$content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));
Unlike other answers, this supports escapes, e.g. "string with " quote in it"
.
$content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));
answered Jun 19 '09 at 10:52
KornelKornel
75.9k28171233
75.9k28171233
add a comment |
add a comment |
The regular expression '"([^\"]+)"'
will match anything between two double quotes.
$string = '"Your Balance left $0.10", End 0';
preg_match('"([^\"]+)"', $string, $result);
echo $result[0];
add a comment |
The regular expression '"([^\"]+)"'
will match anything between two double quotes.
$string = '"Your Balance left $0.10", End 0';
preg_match('"([^\"]+)"', $string, $result);
echo $result[0];
add a comment |
The regular expression '"([^\"]+)"'
will match anything between two double quotes.
$string = '"Your Balance left $0.10", End 0';
preg_match('"([^\"]+)"', $string, $result);
echo $result[0];
The regular expression '"([^\"]+)"'
will match anything between two double quotes.
$string = '"Your Balance left $0.10", End 0';
preg_match('"([^\"]+)"', $string, $result);
echo $result[0];
answered Jun 19 '09 at 9:26
Rich AdamsRich Adams
20.7k43058
20.7k43058
add a comment |
add a comment |
Just use str_replace and escape the quote:
str_replace(""","",$yourString);
Edit:
Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.
add a comment |
Just use str_replace and escape the quote:
str_replace(""","",$yourString);
Edit:
Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.
add a comment |
Just use str_replace and escape the quote:
str_replace(""","",$yourString);
Edit:
Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.
Just use str_replace and escape the quote:
str_replace(""","",$yourString);
Edit:
Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.
answered Jun 19 '09 at 9:19
PaulJWilliamsPaulJWilliams
16.3k14476
16.3k14476
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1017051%2fphp-to-extract-a-string-from-double-quote%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
E,pz15LR fk QOuOM425wmh,YgL1s,jBlyOBPnU5HdUAb02g3,qPh6u2FVllDDbWqhzGyB6uHizi74PxwDRFEM0xib,X4hw,He
You might find
s($str)->between('"', '"')
helpful, as found in this standalone library.– caw
Jul 27 '16 at 2:34