查找并替换所有出现的字符串[php短代码]
我用这段代码用包含图像的链接替换CMS中的短代码,但它只替换第一个短代码查找并替换所有出现的字符串[php短代码],php,regex,loops,str-replace,strstr,Php,Regex,Loops,Str Replace,Strstr,我用这段代码用包含图像的链接替换CMS中的短代码,但它只替换第一个短代码 $string = $row['Content']; if(stristr($string,'[gal=')){ $startTag = "[gal="; $endTag = "]"; $pos1 = strpos($string, $startTag) + strlen($startTag); $pos2 = strpos($string, $endTag); $gal = s
$string = $row['Content'];
if(stristr($string,'[gal=')){
$startTag = "[gal=";
$endTag = "]";
$pos1 = strpos($string, $startTag) + strlen($startTag);
$pos2 = strpos($string, $endTag);
$gal = substr($string, $pos1, $pos2-$pos1);
$q=$db->prepare("select * from images where Gal_ID = :gal");
$q->execute(["gal"=>$gal]);
$imgs='';
while($r=$q->fetch(PDO::FETCH_ASSOC)){
$images[] = $r['Image'];
}
foreach($images as $val){
$imgs .= "<a href='gallery/large/$val' class='fancybox-thumbs' rel='gallery'><img src='gallery/thumb/$val'></a>";
}
$result = substr_replace($string, $imgs, $pos1, $pos2-$pos1);
$result = str_replace($startTag,'',$result);
$result = str_replace($endTag,'',$result);
echo $result;
}
else{
echo $string;
}
结果是只用链接和图像替换第一个短代码,但第二个短代码显示如下:“37”只是数字。因此,如何循环遍历所有短代码,并用链接替换它们,而不仅仅是第一个短代码,这里是我上面描述的完整示例
$string = $row['Content'];
if(stristr($string,'[gal=')){
$startTag = "[gal=";
$endTag = "]";
$pos1 = strpos($string, $startTag) + strlen($startTag);
$pos2 = strpos($string, $endTag);
$gal = substr($string, $pos1, $pos2-$pos1);
$q=$db->prepare("select * from images where Gal_ID = :gal");
$q->execute(["gal"=>$gal]);
$imgs='';
while($r=$q->fetch(PDO::FETCH_ASSOC)){
$images[] = $r['Image'];
}
foreach($images as $val){
$imgs .= "<a href='gallery/large/$val' class='fancybox-thumbs' rel='gallery'><img src='gallery/thumb/$val'></a>";
}
$result = substr_replace($string, $imgs, $pos1, $pos2-$pos1);
$result = str_replace($startTag,'',$result);
$result = str_replace($endTag,'',$result);
echo $result;
}
else{
echo $string;
}
//get matches
if(preg_match_all('/\[gal=(\d+)\]/i', $string, $matches) > 0){
//query for all images. You could/should bind this, but since the expression
//matches only numbers, it is technically not possible to inject anything.
//However best practices are going to be "always bind".
$q=$db->prepare("select Gal_ID, Image from images where Gal_ID in (".implode(',', $matches[1]).")");
$q->execute();
//format the images into an array
$images = array();
while($r=$q->fetch(PDO::FETCH_ASSOC)){
$images[$r['Gal_ID']][] = "<a href='gallery/large/{$r['Image']}' class='fancybox-thumbs' rel='gallery'><img src='gallery/thumb/{$r['Image']}'></a>";
}
//replace shortcode with images
$result = preg_replace_callback('/\[gal=(\d+)\]/i', function($match) use ($images){
if(isset($images[$match[1]])){
return implode('', $images[$match[1]]);
} else {
return $match[0];
}
}, $string);
echo $result;
}
//获取匹配项
如果(preg_match_all('/\[gal=(\d+)\]/i',$string,$matches)>0){
//查询所有图像。您可以/应该绑定此,但由于表达式
//只匹配数字,技术上不可能注入任何内容。
//然而,最佳实践将是“始终绑定”。
$q=$db->prepare(“从Gal_ID所在的图像中选择Gal_ID,“.inplade(',',$matches[1])”);
$q->execute();
//将图像格式化为数组
$images=array();
而($r=$q->fetch(PDO::fetch_ASSOC)){
$images[$r['Gal_ID'][]=”;
}
//用图像替换短代码
$result=preg\u replace\u回调('/\[gal=(\d+)\]/i',函数($match)使用($images){
如果(isset($images[$match[1]])){
返回内爆(“”,$images[$match[1]]);
}否则{
返回$match[0];
}
},$string);
回声$结果;
}
我尽可能多地测试了它,但我没有PDO和/或您的表。这应该是对上面所述内容的一个相当大的替换。这应该通过正则表达式替换来完成,以便它替换所有实例,而不仅仅是第一个实例。如果没有人帮忙的话,我会晚一点回来为你写这篇文章。我建议使用正则表达式来匹配短代码。就我个人而言,我会预先匹配所有现有代码,将它们组合成一个查询,运行该查询并将其格式化为一个数组。然后preg_replace_回调,以查找结果数组来替换短代码。您最终会调用两个regex模式,但只有一个查询是最慢的部分。
$q=$db->prepare(“select*from image,其中Gal_ID='$Gal')代码>警告:内部注入。这并不是因为您使用的是PDO::prepare
,您可以安全地将值串入查询。您应该在Gal_ID=:ID
和$q->execute([':ID'=>$Gal])的图像中使用SELECT*代码>Elias Van Ootegem:我只是插入了变量而没有绑定,只是为了解释,但我总是使用bindParam来保护查询,非常感谢您。