2015-03-19 69 views
-2

我在正则表达式上很糟糕,任何人都可以帮助我吗?正则表达式 - 返回一个字符串

我在VAR $递减产品描述,是这样的:

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:”。如何实现这一目标?

+1

你有没有尝试过或做过一些研究? – Rizier123 2015-03-19 11:45:45

+1

请发布您的尝试不工作。这将是第一步,因此人们可以帮助调整你已有的东西。 – Crackertastic 2015-03-19 11:45:49

+0

我什么也没做,因此不知道如何去实现它。 – dantey89 2015-03-19 11:51:35

回答

1

在PHP中,去匹配任何正则表达式,我们使用preg_matchpreg_match_all功能:

<?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

相匹配,您可以了解更多关于此功能在这里:http://php.net/manual/en/function.preg-match.php

+0

Thenks,它帮助我很多。 – dantey89 2015-03-19 12:22:05

0
<?php 
$subject = "some text , some text, some text , some text 
some text , some text , some text , some text 
Product sku: 111111 dhgfh 
some text , some text, some text , some text"; 

$pattern = '/Product sku:\s*(?P<product_sku>\d+)/'; 
preg_match($pattern, $subject, $matches); 


if (isset($matches['product_sku'])) { 
    echo 'Product sku: ' . $matches['product_sku']; 
} 
else { 
    echo 'Product sku not found!'; 
} 

Demo

0

请尝试以下代码:

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

希望这可以帮助你!

相关问题