[英]How Can I Remove Multiple Sections of Characters From a String? PHP
我试图从数据库中删除一系列字符串中的所有图像标记。 以下工作正常适用于单个图像:(仅供参考 - 字符串中没有其他标签以/>
结尾)
$query = mysql_query("SELECT * FROM events ORDER BY date ASC");
while($row = mysql_fetch_array($query)){
$content = $row['text'];
if(strpos($content, '<img') !== false){
$point1 = strpos($content, '<img');
$point2 = strpos($content, '/>');
$cleaned = substr_replace($content, "", $point1, $point2-1);
}
echo $cleaned;
}
我试图循环来多次清理字符串,但无济于事。 只删除第一个图片标记,只给我相同的结果:
$query = mysql_query("SELECT * FROM events ORDER BY date ASC");
while($row = mysql_fetch_array($query)){
$content = $row['text'];
if(strpos($content, '<img') !== false){
$img_ct = substr_count($content, '<img');
for($i=0;$i<$img_ct;$i++){
$point1 = strpos($content, '<img');
$point2 = strpos($content, '/>');
$cleaned = substr_replace($content, "", $point1, $point2-1);
}
}
echo $cleaned;
}
有没有办法在线程中删除多个文本部分? 循环还是其他?
顺便说一句,这是我的第一个问题。 喜欢这个网站。 你们都非常帮助我。
你可以使用preg_replace()
。
if(strpos($content, '<img') !== false) {
$cleaned = preg_replace('/<img[^>]+\/>/', '', $content);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.