根据句子结构从字符串中提取文本


Extracting text from a string based on sentence structure

这个问题可能已经被问过了,如果是,我很抱歉,但我一直在搜索,不知道如何表达我的问题。

我有这个文本字符串:

$text = "You have received a message.  The quote request is from Cade Carrier; and is for these items: Tires: 195 60 15 - Direct Input (2).";

我需要退出的是Cade Carrier。我知道客户的名字后面总是跟着一个分号(如上面的示例中所示),并且总是以开头。报价请求来自(在的后面加一个空格)。

我该如何从这句话中提取出我需要的文本?

一个简单的正则表达式就可以:

preg_match('/The quote request is from ([^;]+);/', $text, $match);
echo $match[1];

匹配给定的文本,然后捕获()任何不是^的字符[]一个分号一个或多个+直到分号。

在非正则表达式的问题上,可以像这样提取

$text = "You have received a message.  The quote request is from Cade Carrier; and is for these items: Tires: 195 60 15 - Direct Input (2).";
$text2 = strstr($text, ";", true); // get everything before the first ;
$from = strpos($a, "from ") + 5; // +5 for the 5 chars of "from "
$str = substr($a, $from);
var_dump($str); // string(12) "Cade Carrier"