Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Regex - Fatal编程技术网

PHP使用正则表达式返回子字符串

PHP使用正则表达式返回子字符串,php,regex,Php,Regex,我想从另一个字符串中检索一个子字符串 字符串的开头有两种可能。让我们说“abc”或“def” 这个字符串后面有一个“字符”和一个随机字。这个词后面有一个冒号,然后是一个句子。至少字符串的结尾是“再次” 因此,一些可能性是: abc'randomword:我的句子' 或 def'otherrandomword:我的另一句话' 我想用PHP检索字符串中的句子。 因此,对于上面的示例,应该有我的句子和我的另一个句子 我研究了preg\u replace和preg\u match的使用,但我找不到解决此

我想从另一个字符串中检索一个子字符串

字符串的开头有两种可能。让我们说“abc”或“def” 这个字符串后面有一个“字符”和一个随机字。这个词后面有一个冒号,然后是一个句子。至少字符串的结尾是“再次”

因此,一些可能性是:

abc'randomword:我的句子'
def'otherrandomword:我的另一句话'

我想用PHP检索字符串中的句子。 因此,对于上面的示例,应该有
我的句子
我的另一个句子

我研究了
preg\u replace
preg\u match
的使用,但我找不到解决此问题的方法


有人能帮我解决这个问题吗。我希望我把一切都说清楚了。如果有任何问题,请询问。

您尝试了什么模式的
preg\u match
?如果您提供一个具有预期输出的真实示例输入,这也会有所帮助。这就是我想要的。但是当我使用以下字符串时,
zxc'random:my-station'
。然后它返回我的句子。但它只需要在字符串开头找到
abc
def
的时间。
preg_match("/((\babc\b)|(\bdef\b)) '[a-zA-Z]+: (.*?)'/", $your_string, $matches);
var_dump($matches[4]);