如何 preg_replace 从标识符到带有 CR(LF) 字符的文本结尾
How to preg_replace from indentifier to end of text with CR(LF) characters
我正在尝试将一段文本(实际上是 html)分成两部分,顶部和底部。文本中的 'identifier' (<--#SPLIT#-->) 标记要拆分的位置。
为了得到上面的部分,我有以下 preg_replace 确实有效:
$upper = preg_replace('/<--#SPLIT#-->(\s*.*)*/', '', $text);
这给我留下了“<--#SPLIT#-->”之前的所有文本。
为了获得下半部分,我提出了以下无法正常工作的 preg_replace:
$lower = preg_replace('/(\s*.*)*<--#SPLIT#-->/', '', $text);
这returns一个空字符串。
如何修复第二个?
最好用:
explode('<--#SPLIT#-->', $text);
示例代码:
$text = 'Foo bar<--#SPLIT#-->Baz fez';
$temp = explode('<--#SPLIT#-->', $text);
$upper = $temp[0];
$lower = (count($temp > 1) ? $temp[1] : '');
// $upper == 'Foo bar'
// $lower == 'Baz fez'
我正在尝试将一段文本(实际上是 html)分成两部分,顶部和底部。文本中的 'identifier' (<--#SPLIT#-->) 标记要拆分的位置。
为了得到上面的部分,我有以下 preg_replace 确实有效:
$upper = preg_replace('/<--#SPLIT#-->(\s*.*)*/', '', $text);
这给我留下了“<--#SPLIT#-->”之前的所有文本。
为了获得下半部分,我提出了以下无法正常工作的 preg_replace:
$lower = preg_replace('/(\s*.*)*<--#SPLIT#-->/', '', $text);
这returns一个空字符串。
如何修复第二个?
最好用:
explode('<--#SPLIT#-->', $text);
示例代码:
$text = 'Foo bar<--#SPLIT#-->Baz fez';
$temp = explode('<--#SPLIT#-->', $text);
$upper = $temp[0];
$lower = (count($temp > 1) ? $temp[1] : '');
// $upper == 'Foo bar'
// $lower == 'Baz fez'