php正则表达式匹配和替换
再次。 我正在尝试遍历数据库表并替换旧 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在代码中的这一点:
您正在数组中创建两个条目,其中一个带有 id,后跟文本。
我想你想要:
它可以解释为什么每个事件都会发生两次并且没有任何改变。
调试也显示了错误的值:
echo $post['id'].'--Matched and Replaced
';
并且输出是
--Matched and Replaced
没有显示帖子 ID。At the point in the code:
You are creating two entries in the array, one with the id, followed by the text.
I think you want:
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.首先:这些行将
两个元素添加到
$posts
数组中。这就是为什么每个帖子都会有两个输出。第二:我不认为冒号
:
是一个特殊字符 - 它不需要转义。所以它应该看起来像:First: the lines
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: