Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于句子结构的字符串文本提取_Php_String - Fatal编程技术网

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"