2012-08-05 68 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