[英]Extracting text from a string based on sentence structure
This may be asked already and if so, I apologize, but I've been searching and I'm not sure how to phrase my question. 可能已经有人问过了,如果这样,我很抱歉,但是我一直在搜索,不确定如何表达我的问题。
I have this text 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).";
What I need to pull out is Cade Carrier . 我需要拉的是Cade Carrier 。 I know that the name of the customer will always be followed by a semicolon (as in the sample above), and will always be preceded by The quote request is from (with a space after from ).
我知道客户的名称将始终后跟分号(如上述示例中所示),并始终在其前加引号请求是from (在from后面有一个空格)。
How do I go about pulling out the text that I need from this sentence? 我该如何从这句话中提取需要的文字?
A simple regex will do: 一个简单的正则表达式可以做到:
preg_match('/The quote request is from ([^;]+);/', $text, $match);
echo $match[1];
Match the given text and then capture ()
any character []
that is not ^
a semi-colon one or more +
up to the semi-colon. 匹配给定的文本,然后捕获
()
不是^
的一个或多个+
直到该分号的任何字符[]
。
on a non-regex matter, you can extract like this 在非正则表达式问题上,您可以像这样提取
$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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.