PHP-remove<;img>;来自字符串的标记

PHP-remove<;img>;来自字符串的标记,php,string,Php,String,嘿,我需要从一个字符串中删除所有图像,但我找不到正确的方法 以下是我尝试过的,但不起作用: preg_replace("/<img[^>]+\>/i", "(image) ", $content); echo $content; preg\u replace(“/]+\>/i”,“(图像)”,$content); echo$内容; 有什么想法吗?试着把\放在前面 编辑:我刚刚测试了你的正则表达式,效果很好。这就是我使用的: <? $content = "thi

嘿,我需要从一个字符串中删除所有图像,但我找不到正确的方法

以下是我尝试过的,但不起作用:

preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
preg\u replace(“/]+\>/i”,“(图像)”,$content);
echo$内容;

有什么想法吗?

试着把
\
放在
前面

编辑:我刚刚测试了你的正则表达式,效果很好。这就是我使用的:

<?
    $content = "this is something with an <img src=\"test.png\"/> in it.";
    $content = preg_replace("/<img[^>]+\>/i", "(image) ", $content); 
    echo $content;
?>

结果是:

this is something with an (image) in it. 这是一件有(图像)的东西。
我建议使用该方法。

您需要将结果分配回
$content
,因为
preg\u replace
不会修改原始字符串

$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
$content=preg\u replace(“/]+\>/i”,“(图像)”,$content);

肖恩,它很好用,我刚刚用过这个代码

$content = preg_replace("/<img[^>]+\>/i", " ", $content); 
echo $content;
$content=preg\u replace(“/]”+\>/i“,”,$content);
echo$内容;

//结果只有纯文本。它工作

我想将新闻故事的前300个单词显示为预览,不幸的是,这意味着如果一个故事在前300个单词内有一个图像,那么它就会显示在预览列表中,这真的打乱了我的布局。我使用上面的代码从数据库中获取的字符串中隐藏所有图像,效果非常好

$news = $row_latest_news ['content'];
$news = preg_replace("/<img[^>]+\>/i", "", $news); 
if (strlen($news) > 300){
echo substr($news, 0, strpos($news,' ',300)).'...';
} 
else { 
echo $news; 
}
$news=$row_latest_news['content'];
$news=preg\u replace(“/]+\>/i”,”,$news);
如果(斯特伦($news)>300){
echo substr($news,0,strpos($news,,,300));
} 
否则{
echo$新闻;
}
如果用户输入

<img src="#"> 在数据库表中只需插入此字符#


适用于我

只需使用codeigniter的form_验证类:

strip_image_tags($str).

$this->load->library('form_validation');
$this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');

$content=preg_replace(…,$content);可以使用来表示正则表达式。通常,我使用这个,然后根据我的需要修改代码。你能将(image)之外的代码替换为图像的alt吗?尝试将\放在>前面<代码>预替换(“/]+\>/i”,“(图像)”,$content)这与实际问题有什么不同?他有没有说,他正在使用CodeIgniter?评论使用小标记格式:斜体粗体
代码
。您的评论将始终通知帖子作者。要同时通知之前的评论者,请提及他们的用户名:这会去除所有标记,而不仅仅是img标记。
strip_image_tags($str).

$this->load->library('form_validation');
$this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');