如何从双引号中提取字符串?

发布于 2024-07-25 10:08:29 字数 236 浏览 9 评论 0原文

我有一个字符串:

这是一条文本,“Your Balance left $0.10”,End 0

如何提取双引号之间的字符串并且只有文本(没有双引号):

您的余额还剩 0.10 美元

我已尝试过 preg_match_all() 但没有成功。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寄意 2024-08-01 10:08:29

只要格式保持不变,您就可以使用正则表达式来完成此操作。 "([^"]+)" 将匹配模式

  • 双引号
  • 至少一个非双引号
  • 双引号

[^"]+ 两边的括号意味着该部分将作为单独的组返回。

<?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
2024-08-01 10:08:29

对于每个正在寻找全功能字符串解析器的人,请尝试以下操作:

(?:(?:"(?:\\"|[^"])+")|(?:'(?:\\'|[^'])+'));

在 preg_match 中使用:

$haystack = "something else before 'Lars\' Teststring in quotes' something else after";
preg_match("/(?:(?:\"(?:\\\\\"|[^\"])+\")|(?:'(?:\\\'|[^'])+'))/is",$haystack,$match);

返回:

Array
(
    [0] => 'Lars\' Teststring in quotes'
)

这适用于单引号和双引号字符串片段。

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.

青衫负雪 2024-08-01 10:08:29

试试这个:

preg_match_all('`"([^"]*)"`', $string, $results);

您应该在 $results[1] 中获取所有提取的字符串。

Try this :

preg_match_all('`"([^"]*)"`', $string, $results);

You should get all your extracted strings in $results[1].

失退 2024-08-01 10:08:29

与其他答案不同,这支持转义,例如 "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('/"((?:[^"]|\\\\.)*)"/'));
冷情 2024-08-01 10:08:29

正则表达式 '"([^\\"]+)"' 将匹配两个双引号之间的任何内容。

$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];
合约呢 2024-08-01 10:08:29

只需使用 str_replace 并转义引号:

str_replace("\"","",$yourString);

编辑:

抱歉,没有看到第二个引号后有文本。 在这种情况下,我只需进行 2 次搜索,一次搜索第一个引用,一次搜索第二个引用,然后执行 substr 来额外添加两者之间的所有内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文