Hey, I need to delete all images from a string and I just can't find the right way to do it.
嘿,我需要从字符串中删除所有图像,但我找不到正确的方法。
Here is what I tryed, but it doesn't work:
这是我尝试过的,但它不起作用:
preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
Any ideas?
有任何想法吗?
7 个解决方案
#1
130
Try dropping the \
in front of the >
.
尝试删除>前面的\。
Edit: I just tested your regex and it works fine. This is what I used:
编辑:我刚测试你的正则表达式,它工作正常。这是我用过的:
<?
$content = "this is something with an <img src=\"test.png\"/> in it.";
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
?>
The result is:
结果是:
this is something with an (image) in it.
#2
17
You need to assign the result back to $content
as preg_replace
does not modify the original string.
您需要将结果分配回$ content,因为preg_replace不会修改原始字符串。
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
#4
7
Sean it works fine i've just used this code
肖恩它工作正常我刚刚使用此代码
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
echo $content;
//the result it's only the plain text. It works!!!
//结果它只是纯文本。有用!!!
#5
1
I wanted to display the first 300 words of a news story as a preview which unfortunately meant that if a story had an image within the first 300 words then it was displayed in the list of previews which really messed with my layout. I used the above code to hide all of the images from the string taken from my database and it works wonderfully!
我想将新闻故事的前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;
}
#6
-1
$this->load->helper('security');
$h=mysql_real_escape_string(strip_image_tags($comment));
If user inputs
如果用户输入
<img src="#">
In the database table just insert character this #
在数据库表中只插入字符#
Works for me
适合我
#7
-3
simply use the form_validation class of codeigniter:
只需使用codeigniter的form_validation类:
strip_image_tags($str).
$this->load->library('form_validation');
$this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');
#1
130
Try dropping the \
in front of the >
.
尝试删除>前面的\。
Edit: I just tested your regex and it works fine. This is what I used:
编辑:我刚测试你的正则表达式,它工作正常。这是我用过的:
<?
$content = "this is something with an <img src=\"test.png\"/> in it.";
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
?>
The result is:
结果是:
this is something with an (image) in it.
#2
17
You need to assign the result back to $content
as preg_replace
does not modify the original string.
您需要将结果分配回$ content,因为preg_replace不会修改原始字符串。
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
#3
#4
7
Sean it works fine i've just used this code
肖恩它工作正常我刚刚使用此代码
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
echo $content;
//the result it's only the plain text. It works!!!
//结果它只是纯文本。有用!!!
#5
1
I wanted to display the first 300 words of a news story as a preview which unfortunately meant that if a story had an image within the first 300 words then it was displayed in the list of previews which really messed with my layout. I used the above code to hide all of the images from the string taken from my database and it works wonderfully!
我想将新闻故事的前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;
}
#6
-1
$this->load->helper('security');
$h=mysql_real_escape_string(strip_image_tags($comment));
If user inputs
如果用户输入
<img src="#">
In the database table just insert character this #
在数据库表中只插入字符#
Works for me
适合我
#7
-3
simply use the form_validation class of codeigniter:
只需使用codeigniter的form_validation类:
strip_image_tags($str).
$this->load->library('form_validation');
$this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');