获取其他两个字符串之间的字符串


Get the string between two other strings

我有这个total USD100.75 from字符串。我得到了下一个类似的单词

$s = 'total USD100.75 from';
$r = preg_match_all('/(?<=(total))('s'w*)/',$s,$matches);
print_r($matches[0]);

然而,我想获得两个特定字符串之间的字符串,即totalfrom。我怎样才能拿到中间的绳子?。

您也可以使用regexp,例如

'/total (.*?) from/'

使用substrstrlen:

echo substr($string, strlen("total "), -strlen(" from"));

我不确定您的语言的细节,但在Java:中

(?USD[0-9]+'.[0-9]{2})

会起作用。

  1. "美元",后跟
  2. 一个或多个数字,后跟
  3. 字面意思是"."
  4. 后面跟着两个数字

如果你有逗号,你可以试试:

(?USD[0-9]{1,3}(,?[0-9]{3})*'.[0-9]{2})
  1. "美元"后面跟着1-3个数字,后面跟着
  2. 任意数量的组:一个可选逗号,后跟3个数字,后跟
  3. 小数,后面跟
  4. 两位数字

您可以按空格分解字符串。

$s = 'total USD100.75 from';    
$s1 = explode(" ", $s);
echo $s1[1];

输出

USD100.75

演示

正则表达式是实现这一点的简单方法。您只需要了解基础知识。你将通过以下模式实现你想要的:

total's(.*)'sfrom

@user3272483,请检查下面的答案以获得您的解决方案。

$matches = array();
preg_match("/total (.*?) from/", 'total USD100.75 from', $matches);
print_r($matches);