PHP preg_replace在此代码中无法正常工作

时间:2014-01-02 13:41:19

标签: php regex

我的目的是删除任何没有价值的属性。

这是我的代码:

<?php
$srcTxt = "
|title=
|Row18={{Timeline row
  |from=
  |to=2000
  |1-text= TYRR consolidation
  |1-at= 1904
  |2-text= TYRR and TFC takeover
  |2-at= 1927
  |3-text= private bus services acquisition
  |3-at= 1954
|Row18={{Scale row|
  |from=1840
  |to=2000
  |increment=40
  }}
}} ";

$srcTxt = preg_replace("/^ *.*= *$/m", "", $srcTxt);

echo ($srcTxt);

?>

预期输出是删除没有分配任何值的|title=|from=

这完美地运作here但是当我在我的系统中本地运行时却不是这样。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

<?php
$srcTxt = "
|title=
|Row18={{Timeline row
  |from=
  |to=2000
  |1-text= TYRR consolidation
  |1-at= 1904
  |2-text= TYRR and TFC takeover
  |2-at= 1927
  |3-text= private bus services acquisition
  |3-at= 1954
|Row18={{Scale row|
  |from=1840
  |to=2000
  |increment=40
  }}
}} ";

$srcTxt = trim(preg_replace("/(.+?)=\s*\n/", '', $srcTxt));
echo $srcTxt ;

<强>输出

|Row18={{Timeline row
  |to=2000
  |1-text= TYRR consolidation
  |1-at= 1904
  |2-text= TYRR and TFC takeover
  |2-at= 1927
  |3-text= private bus services acquisition
  |3-at= 1954
|Row18={{Scale row|
  |from=1840
  |to=2000
  |increment=40
  }}
}}