Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP preg_替换为SQL_Php_Mysql_Preg Replace - Fatal编程技术网

PHP preg_替换为SQL

PHP preg_替换为SQL,php,mysql,preg-replace,Php,Mysql,Preg Replace,我试图用数据库调用替换基本CMS中的一个简单命令 用户输入 [gallery id=x] 进入CMS,我希望它消失,找到该图库中包含的图像,并显示它们(为jQuery Cycle插件做好准备) 我可以得到: $pattern = '/\[gallery id=(\w+)\]/i'; $rpl = 'Display Gallery ID ${1}'; $bubble = preg_replace($pattern, $rpl, $bubble); …返回“Display Galler

我试图用数据库调用替换基本CMS中的一个简单命令

用户输入

[gallery id=x]
进入CMS,我希望它消失,找到该图库中包含的图像,并显示它们(为jQuery Cycle插件做好准备)

我可以得到:

$pattern = '/\[gallery id=(\w+)\]/i';
$rpl     = 'Display Gallery ID ${1}';
$bubble  = preg_replace($pattern, $rpl, $bubble);
…返回“Display Gallery 12”(例如)。但是,我需要它来执行此操作:

$sql = "SELECT * FROM galleries INNER JOIN photos ON photos.PhotoGallery=galleries.GalleryID WHERE GalleryID='x'";
$set = mysql_query($sql);
echo '<div id="gallery">';
while($row = mysql_fetch_array($set))
{
 echo '<img src="'.$row['PhotoPath'].'" />';
}
echo '</div>';
$sql=“SELECT*FROM galleries internal JOIN photos.PhotoGallery=galleries.GalleryID,其中GalleryID='x';
$set=mysql\u查询($sql);
回声';
while($row=mysql\u fetch\u array($set))
{
回声';
}
回声';

您可以使用
preg\u replace\u callback
函数,它获取正则表达式找到的每个匹配项,并将其替换为从提供的回调返回的字符串。所以你可以这样做:

function generateGallery($matches) {
    // generate the string here, $matches[1] will be your gallery id
    return "Gallery Content for Gallery " . $matches[1];
}

$pattern = '/\[gallery id=(\w+)\]/i';
$bubble = preg_replace_callback($pattern, generateGallery, $bubble);
使用:


在此之后,ID位于
$matches[1]

中,
mysql.*
函数将被删除。不建议编写新代码,因为它将在将来被删除。相反,要么是or and。那么,请详细说明类似的内容。您只是问如何匹配并返回值,而不是替换它吗?看,我不明白他要什么。底部的查询与顶部的替换有什么关系?是关于如何将x的值代入SQL的问题吗?@JasonMcCreary-改为“需要这样做”。[gallery ID=XXX]命令中的ID号需要传递到SQL语句(选择其中ID=XXX)这做了什么,而普通的
preg_replace()
?他希望根据匹配标记中的ID号执行SQL语句,并以这种方式生成替换。
generateGallery
回调为他提供了一个方便的位置,一旦知道id,他就可以这样做。但这不是一种将查询结果回显到页面的方便方式,除非它们恰好是替换的一部分。他在示例中使用了回显,但他回显的是替换字符串。
preg_match($pattern, $string, $matches);