【问题标题】:How to remove first image from string如何从字符串中删除第一张图片
【发布时间】:2019-03-12 03:07:43
【问题描述】:

我有来自包含图像和文本的数据库的结果。我想删除第一张图片。

例子:

$string = 'This is some text and images 
    <img src="http://linktoimage" border="0">,
    more text and <img src="http://linktoanotherimage"> and so on';

我想删除第一张图片,它并不总是相同的 url。

感谢您的帮助。

【问题讨论】:

  • 因为你的字符串实际上是正则的,你可以用一个简单的正则表达式来完成。请尝试一下,如果/当您遇到困难时,请发布您到目前为止所写的内容。

标签: php


【解决方案1】:
$string = 'This is some text and images 
    <img src="http://linktoimage" border="0">,
    more text and <img src="http://linktoanotherimage"> and so on';

print preg_replace('/<img(.*)>/i','',$string,1);

上面应该返回

This is some text and images 
,
    more text and <img src="http://linktoanotherimage"> and so on

假设您知道它将以空格和换行符为前缀,并以逗号和换行符为后缀(并且您也想删除这些),您可以这样做

print preg_replace("/\n    <img(.*)>\,\n    /i",'',$string,1);

哪个会给你

This is some text and images more text and <img src="http://linktoanotherimage"> and so on

【讨论】:

  • 是的,这个,除了你应该有:preg_replace('//i','',$string,1)
  • @Slavic 让它不区分大小写确实不会有什么坏处。我加了,谢谢。
  • 虽然我的意思是 otehr :) 你在所需文本周围有太多文本 (
  • @Slavic 只要它更好。 :)
  • 经过一番思考,这对于标签中有换行符的情况不太适用。我们如何解决这个问题?
【解决方案2】:

另一个Thread有一个很好的答案

function get_first_image($html){
    require_once('SimpleHTML.class.php')

    $post_dom = str_get_dom($html);

    $first_img = $post_dom->find('img', 0);

    if($first_img !== null) {
        return $first_img->src;
    }

    return null;
}

您可以通过正则表达式来实现,但正则表达式并不真正适合此。

【讨论】:

  • 相反,我认为 html 解析器对于这样一个简单的任务来说太过分了。
  • 我同意斯拉夫语,我认为正则表达式非常适合这个。 No, HTML is not a regular language,但是对于这样的事情,我认为它比加载一个巨大的 HTML 解析器更合适。
  • 它的问题是得到正确的正则表达式字符串,记住它并不总是XXX/img.jpg,你也可能最终解析不是图像的图像,例如 这可能有点矫枉过正,但如果您无法验证或清理输入,这可能是最好的方法;)
  • 不。您的字符串与正则表达式不匹配。 ()
【解决方案3】:
$var = 'This is some text and images 
       <img src="http://linktoimage" border="0">, 
       more text and <img src="http://linktoanotherimage"> and so on';

echo preg_replace('/<img.*?>/', '123', $var, 1);

应该这样做。 ?在正则表达式中是让它变得不贪婪。

【讨论】:

    【解决方案4】:

    作为 RegEx 新手,我倾向于避免它并使用类似的东西:

    $i = strpos($string,'<img');
    if ($i !== false) {
        $j = strpos($string, '>', $i);
        if ($j !== false) $string = substr($string,0,$i) . substr($string,$j);
    }
    

    可能应该是 $i-1 和/或 $j+1 - 我不记得确切应该是什么。

    【讨论】:

    • 摆脱恐惧,拥抱正则表达式——你最终会更快乐。
    • 我知道@KevinDTimm!那里有适合我的 RegEx 支持小组,有饼干和折叠椅之类的吗? :-)
    • 对不起,如果开头有任何其他标签,您的代码将无法工作。条件是删除第一个 img 标签。这不是唯一的缺点。
    【解决方案5】:

    终于搞定了:

    preg_replace('/<img[\S\s]*?>/i','',$txt,1)
    

    这也适用于您可能遇到的情况:

    $string = 'This is some text and images 
        <img 
            src="http://linktoimage" border="0">,
        more text and <img src="http://linktoanotherimage"> and so on';
    

    (标签中的换行符。)

    【讨论】:

      【解决方案6】:

      此代码适用于我

      $html 是您要从中删除第一个图像标签的 html 内容变量。

      $str = preg_replace('/(<img[^>]+>)/i','',$html,1);  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2011-11-20
        • 2020-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多