PHP-从字符串中读取变量,然后用数据库中的变量替换

PHP-从字符串中读取变量,然后用数据库中的变量替换,php,preg-replace,Php,Preg Replace,在我的PHP论坛中,我希望人们只需插入图片的编号/ID即可插入图片(他们可以在在线相册中看到) 我正在寻找一个函数,可以读取字符串从他们的文章,例如 “等等,看看这张惊人的照片[IMG]234[/IMG]是不是太棒了……” 然后在数据库中找到ID为234的图片,并将[IMG]234[/IMG]替换为 preg_替换不起作用:(有人有主意吗? 提前感谢您的帮助您的代码需要进行db调用才能从ID获取路径 使用标记解析器或基本正则表达式匹配\[img][0-9]+\[/img](preg\u mat

在我的PHP论坛中,我希望人们只需插入图片的编号/ID即可插入图片(他们可以在在线相册中看到)

我正在寻找一个函数,可以读取字符串从他们的文章,例如

“等等,看看这张惊人的照片[IMG]234[/IMG]是不是太棒了……”

然后在数据库中找到ID为234的图片,并将[IMG]234[/IMG]替换为

preg_替换不起作用:(有人有主意吗?
提前感谢您的帮助

您的代码需要进行db调用才能从ID获取路径

使用标记解析器或基本正则表达式匹配
\[img][0-9]+\[/img]
(preg\u match)

然后在数据库中查询以匹配结果作为ID的路径

最后,使用
str\u replace
将原始“[img]$match[/img]”替换为

示例(pseudo,首先查找这些函数)


注意:这只适用于小写的标记:img,而不适用于img、img、img等等。

您的代码需要进行db调用才能从ID获取路径

使用标记解析器或基本正则表达式匹配
\[img][0-9]+\[/img]
(preg\u match)

然后在数据库中查询以匹配结果作为ID的路径

最后,使用
str\u replace
将原始“[img]$match[/img]”替换为

示例(pseudo,首先查找这些函数)


注意:这只适用于小写的标记:img,而不适用于img、img、img等等。

为什么preg_不替换work?不知道细节,我想你没有逃过方括号。你的正则表达式是什么样子的?试试类似的方法

/\[IMG\](\d+)\[/IMG]/
然后,图像id将位于组1中,即$1

例如:

preg_match_all('/\[IMG\](\d+)\[/IMG]/', $post, $matches);

foreach ($matches as $match) {
    echo "Image number: " . $match[1] . "\n";
}

为什么preg_不代替work?不知道细节,我猜你没有逃过方括号。你的正则表达式是什么样子的?试试类似的东西

/\[IMG\](\d+)\[/IMG]/
然后,图像id将位于组1中,即$1

例如:

preg_match_all('/\[IMG\](\d+)\[/IMG]/', $post, $matches);

foreach ($matches as $match) {
    echo "Image number: " . $match[1] . "\n";
}

我会这样做:

$images = array();
$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) use(&$images) {
    $images[] = $matches[1];
    return '__image_' . $matches[1];
}, $post);

if (count($images)) {
    // Select images
    $imageIds = implode(',', $images);

    // DB query
    $res = mysql_query("SELECT id, path FROM post_images WHERE id IN ({$imageIds})") or die(mysql_error());

    // Replace
    while (($row = mysql_fetch_assoc($res))) {
        $post = str_replace('__image_'.$row['id'], '<img src=' . $row['path'] . ' />', $post);
    }
}
$images=array();
$post=preg\u replace\u回调('\[img\](\d+\[/img\]]i',函数($matches)使用(&$images){
$images[]=$matches[1];
返回“_图像”.$matches[1];
}美元(员额);
如果(计数($images)){
//选择图像
$imageIds=内爆(“,”,$images);
//数据库查询
$res=mysql_query(“从post_图像中选择id,路径,其中id位于({$imageIds})”中)或die(mysql_error());
//替换
而($row=mysql\u fetch\u assoc($res))){
$post=str_replace('''.'图像'.$row['id'],''.$post);
}
}
优点是只对数据库进行一次查询。这对于最小化查询以提高性能总是很重要的。如果您不在意,或者您确信中的图像数量不会太多,可以简单地使用以下代码:

$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) {
    $res = mysql_query("SELECT path FROM post_images WHERE id = {$matches[1]}") or die(mysql_error());
    $path = mysql_result($res, 0);
    return "<img src='$path' />";
}, $post);
$post=preg\u replace\u回调('\[img\](\d+\[/img\]]i',函数($matches){
$res=mysql_query(“从post_图像中选择路径,其中id={$matches[1]}”)或die(mysql_error());
$path=mysql\u结果($res,0);
返回“”;
}美元(员额);

我会这样做:

$images = array();
$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) use(&$images) {
    $images[] = $matches[1];
    return '__image_' . $matches[1];
}, $post);

if (count($images)) {
    // Select images
    $imageIds = implode(',', $images);

    // DB query
    $res = mysql_query("SELECT id, path FROM post_images WHERE id IN ({$imageIds})") or die(mysql_error());

    // Replace
    while (($row = mysql_fetch_assoc($res))) {
        $post = str_replace('__image_'.$row['id'], '<img src=' . $row['path'] . ' />', $post);
    }
}
$images=array();
$post=preg\u replace\u回调('\[img\](\d+\[/img\]]i',函数($matches)使用(&$images){
$images[]=$matches[1];
返回“_图像”.$matches[1];
}美元(员额);
如果(计数($images)){
//选择图像
$imageIds=内爆(“,”,$images);
//数据库查询
$res=mysql_query(“从post_图像中选择id,路径,其中id位于({$imageIds})”中)或die(mysql_error());
//替换
而($row=mysql\u fetch\u assoc($res))){
$post=str_replace('''.'图像'.$row['id'],''.$post);
}
}
优点是只对数据库进行一次查询。这对于最小化查询以提高性能总是很重要的。如果您不在意,或者您确信中的图像数量不会太多,可以简单地使用以下代码:

$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) {
    $res = mysql_query("SELECT path FROM post_images WHERE id = {$matches[1]}") or die(mysql_error());
    $path = mysql_result($res, 0);
    return "<img src='$path' />";
}, $post);
$post=preg\u replace\u回调('\[img\](\d+\[/img\]]i',函数($matches){
$res=mysql_query(“从post_图像中选择路径,其中id={$matches[1]}”)或die(mysql_error());
$path=mysql\u结果($res,0);
返回“”;
}美元(员额);
以下是我所做的:

$find = preg_match_all("!\[img\][0-9]+\[\/img\]!", $post, $matches);
foreach ($matches as $match) {
    foreach ($match as $ma) {
        $res = str_replace("[/img]","", str_replace("[img]", "",$ma));
        $query = "
            SELECT 
                path
            FROM
                table
            WHERE id = '".$res."'   
        ";
        $result = mysql_query($query, $conn) or die(mysql_error());
        while($line = mysql_fetch_array($result)) { 
            $path = $line["path"];  
            $path = "<img src = '".$path. "'></img>";
            $post = str_replace ("[img]" . $res . "[/img]", $path, $post);
        }
    }
}
$find=preg\u match\u all(!\[img\][0-9]+\[\/img\]!”,$post$matches);
foreach($matches作为$match进行匹配){
foreach($ma匹配){
$res=str_replace(“[/img]”,“”,str_replace(“[img]”,“”,$ma));
$query=”
选择
路径
从…起
桌子
其中id=“$res.”
";
$result=mysql\u query($query,$conn)或die(mysql\u error());
而($line=mysql\u fetch\u数组($result)){
$path=$line[“path”];
$path=“”;
$post=str_replace(“[img]”,$res.[/img]”,$path,$post);
}
}
}
注意:我不知道为什么preg_match_all会创建一个2深度数组

$find = preg_match_all("!\[img\][0-9]+\[\/img\]!", $post, $matches);
foreach ($matches as $match) {
    foreach ($match as $ma) {
        $res = str_replace("[/img]","", str_replace("[img]", "",$ma));
        $query = "
            SELECT 
                path
            FROM
                table
            WHERE id = '".$res."'   
        ";
        $result = mysql_query($query, $conn) or die(mysql_error());
        while($line = mysql_fetch_array($result)) { 
            $path = $line["path"];  
            $path = "<img src = '".$path. "'></img>";
            $post = str_replace ("[img]" . $res . "[/img]", $path, $post);
        }
    }
}
$find=preg\u match\u all(!\[img\][0-9]+\[\/img\]!”,$post$matches);
foreach($matches作为$match进行匹配){
foreach($ma匹配){
$res=str_replace(“[/img]”,“”,str_replace(“[img]”,“”,$ma));
$query=”
选择
路径
从…起
桌子
其中id=“$res.”
";
$result=mysql\u query($query,$conn)或die(mysql\u error());
而($line=mysql\u fetch\u数组($result)){
$path=$line[“path”];
$path=“”;
$post=str_replace(“[img]”,$res.[/img]”,$path,$post);
}
}
}

注意:我不知道为什么preg_match_all会创建一个2深度数组

您可以发布您到目前为止尝试过的内容吗?感觉您正在尝试获得一个完整的解决方案,而这不是StackOverflow的目的:)不是真正的完整解决方案,只是关键想法;)你能把你到目前为止试过的东西贴出来吗?It费用