PCRE 的 sed 等效语法


sed equivalent syntax for pcre

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

$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