PHP从双引号提取字符串 [英] php to extract a string from double quote

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

问题描述

我有一个字符串:

这是一个文本,您的余额还剩$ 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

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

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