Php 正则表达式-返回字符串

Php 正则表达式-返回字符串,php,regex,Php,Regex,我对正则表达式很在行,有人能帮我吗 我在var$desc中有产品描述,类似这样: some text , some text, some text , some text some text , some text , some text , some text Product sku: 111111 some text , some text, some text , some text 我需要的是在文本“产品sku:”后返回一个数字。如何实现这一点?在PHP中,为了匹配任何正则表达式,我们

我对正则表达式很在行,有人能帮我吗

我在var$desc中有产品描述,类似这样:

some text , some text, some text , some text
some text , some text , some text , some text
Product sku: 111111
some text , some text, some text , some text

我需要的是在文本“产品sku:”后返回一个数字。如何实现这一点?

在PHP中,为了匹配任何正则表达式,我们使用
preg\u match
preg\u match\u所有函数:

<?php
preg_match('/Product sku:[\s]*([\d]+)/i', 'some text , some text, some text , some text
some text , some text , some text , some text
Product SKU: 111111
some text , some text, some text , some text', $matches);
print_r($matches);

/**
Output:
Array ( [0] => Product SKU: 111111 [1] => 111111 ) // $matches[1] is what you need
*/

?>

注意正则表达式中的
i
,它表示大小写不敏感。所以 它与
sku
sku


您可以在此处阅读有关此函数的更多信息:

请尝试以下代码:

if(preg_match('/Product sku:\s*?(\d+)/mi',$strs,$matchs)){                                                                                                    
    echo $matchs[1];
}

希望这能对您有所帮助!

您尝试过什么或做过一些研究吗?请发布您的尝试,但没有效果。这将是第一步,这样人们可以帮助调整你已经拥有的。我什么也没做,因为我不知道如何实现它。在我回答之前,有一个问题:SKU将永远是一个数字?总是在第三行吗?SKU行中不会提及其他细节?是的,它总是在“产品SKU”之后,而且总是在“产品SKU”之后,这对我很有帮助。
if(preg_match('/Product sku:\s*?(\d+)/mi',$strs,$matchs)){                                                                                                    
    echo $matchs[1];
}