用图像替换动态文本
我正在尝试创建一个简化的代码,以根据用户输入将图像动态插入到页面中,类似于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试使用
preg_match
来获取 image_url 和preg_replace
来替换它:编辑
I would try doing it with
preg_match
to get the image_url andpreg_replace
to replace it:edit