【发布时间】:2017-09-25 17:59:19
【问题描述】:
我正在从我的 Wordpress 数据库中检索一个字符串;我检索的字符串是一个纯文本字段,可以在其中添加 Wordpress 库。字符串的一些示例如下:
<p>test</p><p>[gallery columns=\"2\" ids=\"705,729\"]</p>
<p>[gallery columns=\"2\" ids=\"696,694\"]</p>
<p>test</p>
我想检索ids=\"x,x\" 字段中的数字。
我有以下代码:
for ($i = 1; $i<5; $i++) {
$result = get_ids_per_category($i, getReferencesMapId());
${"idArray".$i} = array();
foreach ($result as $res) {
$subject = $res->description;
$pattern = "/\[(.*?)\]/";
preg_match($pattern,$subject,$matches);
if ($matches[1]) {
$subject2 = $matches[1];
$pattern2 = '/ids=\\"(.*)\\"/';
preg_match($pattern2, $subject2, $matches2);
array_push( ${"idArray".$i}, $matches2);
}
}
if (!empty(${"idArray".$i})) {
${"finalArray".$i} = array();
foreach (${"idArray".$i} as $arr) {
$newarray = explode(",",$arr[1]);
foreach ($newarray as $item) {
array_push( ${"finalArray".$i}, $item);
}
}
}
}
如果我调用var_dump($subject2),会返回以下结果:
\page-referentiemap.php:58:string 'gallery columns=\"2\" ids=\"477,476\"' (length=37)
\page-referentiemap.php:58:string 'gallery columns=\"1\" ids=\"690\"' (length=33)
\page-referentiemap.php:58:string 'gallery ids=\"688,689,690\"' (length=27)
\page-referentiemap.php:58:string 'gallery columns=\"2\" ids=\"697,698,699\"' (length=41)
\page-referentiemap.php:58:string 'gallery ids=\"702,701,703\"' (length=27)
\page-referentiemap.php:58:string 'gallery columns=\"2\" ids=\"696,694\"' (length=37)
到目前为止一切顺利,但之后我创建正则表达式的行如下:
preg_match($pattern2, $subject2, $matches2);
将始终在 $matches2 中返回空值。
我不记得我在过去几周内更改了任何代码,这确实有效。谁能告诉我我错过了什么?
【问题讨论】: