2012-08-05 23 views
-3

基本上我使用bash shell脚本cat new | sed '/1.&nbsp/,/<div/!d' > new2来提取文本从1.&nbsp开始,并在第一次出现<div结束。然后将其保存到new2文件中。如何在php中使用pcre做同样的工作。sed等效语法为pcre

回答

1
$text = file_get_contents('php://stdin'); 
$matches = array(); 
if(preg_match('/1\.&nbsp(.*?)<div/', $text, $matches)) { 
    echo $matches[1]; 
} 

测试:

echo 'abc 1.&nbsp;This is a test<div>more stuff<div>and more' | php test.php 
;This is a test