Php 基于句子结构的字符串文本提取
这可能已经被问到了,如果是这样,我道歉,但我一直在搜索,我不知道如何表达我的问题 我有以下文本字符串:Php 基于句子结构的字符串文本提取,php,string,Php,String,这可能已经被问到了,如果是这样,我道歉,但我一直在搜索,我不知道如何表达我的问题 我有以下文本字符串: $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)."; 我需要的是凯德·卡利。我知道客户的名字后面总是跟一个分号(如上面的示例所示),并且前面总是跟quote req
$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).";
我需要的是凯德·卡利。我知道客户的名字后面总是跟一个分号(如上面的示例所示),并且前面总是跟quote request is from(在from后面加一个空格)
我该如何从这个句子中提取出我需要的文本 一个简单的正则表达式可以:
preg_match('/The quote request is from ([^;]+);/', $text, $match);
echo $match[1];
匹配给定的文本,然后捕获任何不是分号的字符
()
[]。^
一个或多个+
到分号。简单的正则表达式可以:
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"
在非正则表达式的情况下,可以这样提取
$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"