用图像替换动态文本

发布于 2024-10-16 15:32:43 字数 1840 浏览 4 评论 0原文

我正在尝试创建一个简化的代码,以根据用户输入将图像动态插入到页面中,类似于 BBCode。

例如,如果我的一个用户输入“我喜欢鸭子[image]ducks[/image]”,我想爆炸[image]ducks[/image],在MySQL中搜索关键字“ducks”,拉取图像路径&数据库中匹配的名称,然后显示图像 HTML 代码以及图像的源。

function image_replace($dimg){
    list($title) = explode("[image]",$dimg);
    $query_image = mysql_query("SELECT * FROM images WHERE image_title LIKE '%$title%'");
    $fetch_image = mysql_fetch_array($query_image);
    $image_path = $fetch_image['image_path'];
    $image_filename = $fetch_image['image_filename'];
    $image_source = $image_path.$image_filename;
    $dimg = str_replace("[image]","<img src=\"$image_source\">", $dimg);
    $dimg = str_replace("[/image]","</img>", $dimg);
    $dimg = str_replace("$title", "", $dimg);
    return $img;
  }

image_replace($ducks);

我遇到的难题是如何替换动态生成的页面中的文本(如果存在),以及如何在代码不存在时保留内容。有什么想法吗?


编辑 - 使问题复杂化:

感谢您的帮助!我使用您的输入来创建以下函数:

function image_replace($string){
    $matches = array();
    preg_match('/\[image\](.*)\[\/image\]/', $string, $matches);
    $image = $matches[1];
    $query_image = mysql_query("SELECT * FROM images WHERE image_title LIKE '%$image%'");
    $fetch_image = mysql_fetch_array($query_image);
    $image_path = $fetch_image['image_path'];
    $image_filename = $fetch_image['image_filename'];
    $image_source = $image_path.$image_filename;
    $image_url = "<img src=\"$image_source\"></img>";
    $new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string);

    return $new_string;
  }

无论发生多少次,我都需要它工作(因此,如果我的用户写[image]duck[/image],然后两句话后写[image]cow[/image],我希望该函数将两者替换为各自的结果)。就目前情况而言,对于多个实例,它会出错(不是有效的 SQL 资源),这是有道理的,因为 preg_match 只查找一个。我尝试创建一个循环(while & foreach w/ preg_match_all)来尝试测试这个概念 - 两者都创建了无限循环,而我的网络服务器管理员不太高兴:p

I am trying to create a simplified code to insert images dynamically into a page based on user entry similar to BBCode.

For example, if one of my users types "I like ducks [image]ducks[/image]", I want to explode the [image]ducks[/image], search MySQL for the keyword "ducks", pull the image path & name from the database that matches, then display the image HTML code as well as the source to the image.

function image_replace($dimg){
    list($title) = explode("[image]",$dimg);
    $query_image = mysql_query("SELECT * FROM images WHERE image_title LIKE '%$title%'");
    $fetch_image = mysql_fetch_array($query_image);
    $image_path = $fetch_image['image_path'];
    $image_filename = $fetch_image['image_filename'];
    $image_source = $image_path.$image_filename;
    $dimg = str_replace("[image]","<img src=\"$image_source\">", $dimg);
    $dimg = str_replace("[/image]","</img>", $dimg);
    $dimg = str_replace("$title", "", $dimg);
    return $img;
  }

image_replace($ducks);

The wall I'm hitting is how to replace the text inside a dynamically generated page if it exists - and leave the content alone if the code doesn't exist. Any ideas?


EDIT - Complicating the problem:

Thanks for helping! I used your input to make the following function:

function image_replace($string){
    $matches = array();
    preg_match('/\[image\](.*)\[\/image\]/', $string, $matches);
    $image = $matches[1];
    $query_image = mysql_query("SELECT * FROM images WHERE image_title LIKE '%$image%'");
    $fetch_image = mysql_fetch_array($query_image);
    $image_path = $fetch_image['image_path'];
    $image_filename = $fetch_image['image_filename'];
    $image_source = $image_path.$image_filename;
    $image_url = "<img src=\"$image_source\"></img>";
    $new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string);

    return $new_string;
  }

I need this to work regardless of how many instances it occurs (thus if my user writes [image]duck[/image] then two sentences later writes [image]cow[/image], I want the function to replace both with their respective result). As it stands now, with more than one instance, it errors (not a valid SQL resource) which makes sense since preg_match only looks for one. I tried creating a loop (while & foreach w/ preg_match_all) to try testing the concept - both created infinite loops and my web server admin isn't too happy :p

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

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

发布评论

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

评论(1

表情可笑 2024-10-23 15:32:43

我会尝试使用 preg_match 来获取 image_url 和 preg_replace 来替换它:

$string = 'I like ducks [image]ducks[/image]';
echo 'Before: ' . $string . '<br />';
$matches = array();
preg_match('/\[image\](.*)\[\/image\]/', $string, $matches);
$image = $matches[1];
//Lookup image_url and throw it in an <img>
$image_url = 'http://blah.com'; //Don't forget <img>
$new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string);
echo 'After: ' . $new_string;

编辑

$string = "<br />I like ducks [image]ducks[/image]<br />I like cows [image]cows[/image]<br />I like pigs [image]pigs[/image]";
echo 'Before: ' . $string . '<br />';

$matches = array();
preg_match_all('/\[image\]([^\[]*)\[\/image\]/', $string, $matches);
$image_names = $matches[1];
foreach($image_names as $image_name) {
    //Perform your lookup on each name
    //If it is valid perform the replace
    //Gonna say cows isn't there to test
    if($image_name != 'cows') {
        $image_url = 'http://blah.com'; //result of lookup
        $string = preg_replace('/\[image\]' . $image_name . '\[\/image\]/', $image_url, $string);
    }
}
echo 'After: ' . $string;

I would try doing it with preg_match to get the image_url and preg_replace to replace it:

$string = 'I like ducks [image]ducks[/image]';
echo 'Before: ' . $string . '<br />';
$matches = array();
preg_match('/\[image\](.*)\[\/image\]/', $string, $matches);
$image = $matches[1];
//Lookup image_url and throw it in an <img>
$image_url = 'http://blah.com'; //Don't forget <img>
$new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string);
echo 'After: ' . $new_string;

edit

$string = "<br />I like ducks [image]ducks[/image]<br />I like cows [image]cows[/image]<br />I like pigs [image]pigs[/image]";
echo 'Before: ' . $string . '<br />';

$matches = array();
preg_match_all('/\[image\]([^\[]*)\[\/image\]/', $string, $matches);
$image_names = $matches[1];
foreach($image_names as $image_name) {
    //Perform your lookup on each name
    //If it is valid perform the replace
    //Gonna say cows isn't there to test
    if($image_name != 'cows') {
        $image_url = 'http://blah.com'; //result of lookup
        $string = preg_replace('/\[image\]' . $image_name . '\[\/image\]/', $image_url, $string);
    }
}
echo 'After: ' . $string;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文