Php 将图像url转换为完整的html图像标记
我的评论工作的事情,我希望我的用户能够张贴图像的网址,我将改变成带有标签的图像网址 我的职能Php 将图像url转换为完整的html图像标记,php,Php,我的评论工作的事情,我希望我的用户能够张贴图像的网址,我将改变成带有标签的图像网址 我的职能 function ImageTag($text) { // The Regular Expression filter $reg_exUrl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; // The Text you want to filter for urls if(preg_mat
function ImageTag($text) {
// The Regular Expression filter
$reg_exUrl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
if(preg_match_all($reg_exUrl, $text, $url)) {
// make the urls hyper links
$matches = array_unique($url[0]);
foreach($matches as $match) {
$check = substr($match, -4);
if ($check == ".jpg" || $check == ".png" || $check == ".gif" || $check == "jpeg" || $check == ".JPG" || $check == ".PNG" || $check == ".GIF" || $check == "JPEG"){
$replacement = "<br/><img src=$match>";
$text = str_replace($match, $replacement, $text);
}
}
return nl2br($text);
} else {
// if no urls in the text just return the text
return nl2br($text);
}
}
$text = "The text you want to filter goes here. <img src='http://static.php.net/www.php.net/images/php.gif'> http://www.facebook.com http://static.php.net/www.php.net/images/php.gif";
$content = ImageTag($text);
echo $content;
函数图像标签($text){
//正则表达式过滤器
$reg_exUrl=“/(http | https)\:\/\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
//要筛选URL的文本
if(preg_match_all($reg_exUrl,$text,$url)){
//使URL成为超链接
$matches=array_unique($url[0]);
foreach($matches作为$match进行匹配){
$check=substr($match,-4);
如果($check==”.jpg“| |$check==”.png“| |$check==”.gif“| |$check==”.jpg“| |$check=”.png“| |$check=”.gif“| |$check==”.jpeg”){
$replacement=“
”;
$text=str_replace($match,$replacement,$text);
}
}
返回nl2br($text);
}否则{
//如果文本中没有URL,请返回文本
返回nl2br($text);
}
}
$text=“要筛选的文本位于此处。http://www.facebook.com http://static.php.net/www.php.net/images/php.gif";
$content=ImageTag($text);
echo$内容;
我的函数完成了一半,但我不知道如何解决已经有html标记的图像
这就是我得到的
源代码:
The text you want to filter goes here. <img src="<br/><img src=http://static.php.net/www.php.net/images/php.gif>"> http://www.facebook.com <br><img src="http://static.php.net/www.php.net/images/php.gif">
要筛选的文本将显示在此处。http://www.facebook.com
您可以先解码html标记(),然后去除标记(),并用空白替换“src=”为什么不为图像使用数据uri?我不知道关于数据uri的任何信息,但我会检查,而不是/除了发布屏幕截图,发布原始(用户贡献的)文本和生成的html,会更有帮助的。我贴了谢谢@Passerby@deerox是否允许用户贡献HTML内容?