如何从双引号中提取字符串?
我有一个字符串:
这是一条文本,“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只要格式保持不变,您就可以使用正则表达式来完成此操作。
"([^"]+)"
将匹配模式[^"]+
两边的括号意味着该部分将作为单独的组返回。As long as the format stays the same you can do this using a regular expression.
"([^"]+)"
will match the patternThe brackets around the
[^"]+
means that that portion will be returned as a separate group.对于每个正在寻找全功能字符串解析器的人,请尝试以下操作:
在 preg_match 中使用:
返回:
这适用于单引号和双引号字符串片段。
For everyone hunting for a full featured string parser, try this:
Use in preg_match:
Returns:
This works with single and double quoted string fragments.
试试这个:
您应该在 $results[1] 中获取所有提取的字符串。
Try this :
You should get all your extracted strings in $results[1].
与其他答案不同,这支持转义,例如
"string with \" quote in it"
。Unlike other answers, this supports escapes, e.g.
"string with \" quote in it"
.正则表达式
'"([^\\"]+)"'
将匹配两个双引号之间的任何内容。The regular expression
'"([^\\"]+)"'
will match anything between two double quotes.只需使用 str_replace 并转义引号:
编辑:
抱歉,没有看到第二个引号后有文本。 在这种情况下,我只需进行 2 次搜索,一次搜索第一个引用,一次搜索第二个引用,然后执行 substr 来额外添加两者之间的所有内容。
Just use str_replace and escape the quote:
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.