如何从双引号中提取字符串? [英] How to extract a string from double quotes?

查看:77
本文介绍了如何从双引号中提取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:

这是一段文字,您的余额还剩 0.10 美元",结束 0

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

您的余额还剩 0.10 美元

Your Balance left $0.10

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

I have tried preg_match_all() but with no luck.

推荐答案

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

As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern

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

[^"]+ 周围的括号表示该部分将作为一个单独的组返回.

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

这篇关于如何从双引号中提取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆