pcre的sed等价语法

时间:2012-08-05 19:00:35

标签: php sed

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

1 个答案:

答案 0 :(得分: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
相关问题