我总是使用preg_match它总是工作正常,但今天我试图获得两个HTML标签之间的内容<code: 1>DATA</code>
我有一个问题,我的代码解释了:
function findThis($data){
preg_match_all("/\<code: (.*?)\>(.*?)\<\/code\>/i",$data,$conditions);
return $conditions;
}
// plain text
// working fine
$data1='Some text...Some.. Te<code: 1>This is a php code</code>';
//A text with a new lines
// Not working..
$data2='some text..
some.. te
<code: 1>
This is a php code
..
</code>
';
print_r(findThis($data1));
// OUTPUT
// [0][0] => <code: 1>This is a php code</code>
// [1][0] => 1
// [2][0] => This is a php code
print_r(findThis($data2));
//Outputs nothing!
Run Code Online (Sandbox Code Playgroud)
这是因为.PHP中的字符是除了换行符之外的任何通配符.包括换行符在内的例子会破裂.你想要做的是在模式的末尾添加"s"标志,这会修改.绝对匹配一切(包括换行).
/\<code: (.*?)\>(.*?)\<\/code\>/is
Run Code Online (Sandbox Code Playgroud)
见这里:http://www.php.net/manual/en/regexp.reference.internal-options.php
| 归档时间: |
|
| 查看次数: |
3105 次 |
| 最近记录: |