php正则表达式匹配和替换

发布于 2024-12-06 03:05:47 字数 1221 浏览 0 评论 0原文

再次。 我正在尝试遍历数据库表并替换旧 BBCode 的所有实例(即:[i:fs8d979])并将其替换为简单的 BBCode([i] )。然而,我得到的结果非常令人困惑。

$root_path = './';
include($root_path.'includes/common.php');

$posts = array();
$sql = 'SELECT post_id, post_text FROM posts';
$db->query($sql);
while($row = $db->fetch_assoc())
{
    $posts[]['id'] = $row['post_id'];
    $posts[]['text'] = $row['post_text'];
}

foreach($posts as $post)
{
    $regex = "/\[(\D)(\:[a-zA-Z0-9_]{1,})\]/";

    if(preg_match($regex, $post['text'], $matches))
    {
        $string = preg_replace('/'.$matches[2].'/', '', $post['text']);
        $sql = 'UPDATE posts SET post_text = "'.$string.'" WHERE post_id = '.$post['id'];
        $db->query($sql);
        echo $post['id'].'--Matched and replaced<br />';
    }
    else
    {
        echo $post['id'].'--No Match<br />';
    }
}
echo 'done';

当我运行这个脚本时,我得到这样的输出:

1302--No Match
--No Match
1303--No Match
--No Match
17305--No Match
--Matched and replaced
5532--No Match
--No Match
17304--No Match
--No Match
1310--No Match
--No Match

看起来该脚本正在尝试将所有事情执行两次,但我不确定为什么。数据库字段也没有更新。 为了调试目的,我已经回显了所有内容,并且所有变量都已设置,并且一切看起来都应该正常工作。 有什么建议吗?

again.
I'm trying to go through a database table and replace all instances of old BBCode (ie: [i:fs8d979]) and replace it with simple BBCode ([i]). However, I'm getting very confusing results.

$root_path = './';
include($root_path.'includes/common.php');

$posts = array();
$sql = 'SELECT post_id, post_text FROM posts';
$db->query($sql);
while($row = $db->fetch_assoc())
{
    $posts[]['id'] = $row['post_id'];
    $posts[]['text'] = $row['post_text'];
}

foreach($posts as $post)
{
    $regex = "/\[(\D)(\:[a-zA-Z0-9_]{1,})\]/";

    if(preg_match($regex, $post['text'], $matches))
    {
        $string = preg_replace('/'.$matches[2].'/', '', $post['text']);
        $sql = 'UPDATE posts SET post_text = "'.$string.'" WHERE post_id = '.$post['id'];
        $db->query($sql);
        echo $post['id'].'--Matched and replaced<br />';
    }
    else
    {
        echo $post['id'].'--No Match<br />';
    }
}
echo 'done';

when i run this script, I get output like this:

1302--No Match
--No Match
1303--No Match
--No Match
17305--No Match
--Matched and replaced
5532--No Match
--No Match
17304--No Match
--No Match
1310--No Match
--No Match

it would appear that the script is attempting to do everything twice, and I'm not sure why. The database fields are not getting updated either.
I've echoed everything out for debugging purposes, and all variables are set and everything looks like it should be working properly.
Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2024-12-13 03:05:47

在代码中的这一点:

while($row = $db->fetch_assoc())
{
    $posts[]['id'] = $row['post_id'];
    $posts[]['text'] = $row['post_text'];
}

您正在数组中创建两个条目,其中一个带有 id,后跟文本。

我想你想要:

while($row = $db->fetch_assoc())
{
    $posts[] = array('id' => $row['post_id'], 'text' => $row['post_text']);
}

它可以解释为什么每个事件都会发生两次并且没有任何改变。

调试也显示了错误的值:

echo $post['id'].'--Matched and Replaced
';

并且输出是

--Matched and Replaced 没有显示帖子 ID。

At the point in the code:

while($row = $db->fetch_assoc())
{
    $posts[]['id'] = $row['post_id'];
    $posts[]['text'] = $row['post_text'];
}

You are creating two entries in the array, one with the id, followed by the text.

I think you want:

while($row = $db->fetch_assoc())
{
    $posts[] = array('id' => $row['post_id'], 'text' => $row['post_text']);
}

It would explain why each one is happening twice and nothing is changing.

The debug was showing the wrong value too:

echo $post['id'].'--Matched and replaced<br />';

and the output was

--Matched and replaced which showed no post id.

度的依靠╰つ 2024-12-13 03:05:47

首先:这些行将

$posts[]['id'] = $row['post_id'];
$posts[]['text'] = $row['post_text'];

两个元素添加到 $posts 数组中。这就是为什么每个帖子都会有两个输出。

第二:我不认为冒号 : 是一个特殊字符 - 它不需要转义。所以它应该看起来像:

$regex = "/\[(\D)(:[a-zA-Z0-9_]+)\]/";

First: the lines

$posts[]['id'] = $row['post_id'];
$posts[]['text'] = $row['post_text'];

are adding two elements to the $posts array. That is why you are getting two outputs per post.

Second: I don't think the colon : is a special character - it doesn't need to be escaped. So it should look like:

$regex = "/\[(\D)(:[a-zA-Z0-9_]+)\]/";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文