【问题标题】:how to sub-string from string by specifying the start and end characters? [closed]如何通过指定开始和结束字符从字符串中提取字符串? [关闭]
【发布时间】:2015-08-03 19:51:08
【问题描述】:

我有一些总是以图像标签开头的文本,所以我想通过指定应该删除的字符串的开始和结束字符来打印没有图像的文本并获取其余的文本,喜欢:

explode($text, '<img', '/>'); // where explode($string, $start_chars, $end_chars);

例如:

$text = "&lt;img src='anything' width='100' height='200'/&gt;&lt;h1&gt;Hello World!&lt;/h1&gt;";

输出应该是&lt;h1&gt;Hello World!&lt;/h1&gt;

那么我如何在 php 中做到这一点?

【问题讨论】:

  • 请显示示例文本和所需的输出
  • 可以用字符串函数来做这件事。我会建议正则表达式。在这里讨论:stackoverflow.com/questions/2180255/…
  • @Twisty 希望有更好的解决方案,谢谢
  • 您的示例只需要:strip_tags($text)
  • @Dagon strip_tags() 将删除所有标签,我只想删除第一个 img 标签

标签: php explode string-split


【解决方案1】:

似乎是最佳答案:

$dom = new DOMDocument();
$dom->loadHTML($m->en);
$node = $dom->getElementsByTagName('img')->item(0);
$node->parentNode->removeChild($node);
$string = $dom->saveHTML();
echo $string;

【讨论】:

    【解决方案2】:

    试试这个:

    $text = "<img src='anything' width='100' height='200'/><h1>Hello World!</h1>";
    
    $dom = new DOMDocument();
    $dom->loadHTML($text);
    
    $node = $dom->getElementsByTagName('img')->item(0);
    $node->parentNode->removeChild($node);
    
    $dom->removeChild($dom->doctype);           
    $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
    
    echo $dom->saveHtml();
    

    【讨论】:

      【解决方案3】:

      根据新问题修改...

      使用 DOMDocument:

      $text = "<img src='anything' width='100' height='200'/><h1>Hello World!</h1>";
      $dom = new DOMDocument();
      $dom->loadHTML($text);
      $h1Tags = $dom->getElementsByTagName('h1');
      $string = $dom->saveHTML($h1Tags->item(0));
      echo $string;
      

      输出:&lt;h1&gt;Hello World!&lt;/h1&gt;

      有关更多信息/示例,请参阅here

      【讨论】:

      • 我都试过了,没有人给出要求的结果
      • 从我开始打字的时候起,你就稍微改变了这个问题。看看新答案
      • 天哪,你又改了!!我放弃了。
      【解决方案4】:

      给定一些文本,例如:

      <img src='img1.png' width='100' height='200'/><h1>Title 1</h1>
      <img src='img2.png' width='100' height='200'/><h1>Title 2</h1>
      <img src='img4.png' width='100' height='200'/><h1>Title 3</h1>
      

      您声明您只想收集将出现在 IMG 标签之间的文本。这事先并不清楚,建议您可以使用 DOMDocument 来解析 HTML。

      使用正则表达式是另一种方法。示例:https://regex101.com/r/kH0xA3/1

      $re = "/<*img.*\\/>(.*)/im"; 
      $str = "<img src='img1.png' width='100' height='200'/><h1>Title 1</h1>\n<img src='img2.png' width='100' height='200'/><h1>Title 2</h1>\n<img src='img4.png' width='100' height='200'/><h1>Title 3</h1>"; 
      
      preg_match_all($re, $str, $matches);
      

      【讨论】:

        【解决方案5】:

        您可能没有听说过 PHP 的 substr() 函数。在这里注明!

        http://php.net/substr

        【讨论】:

        • 不鼓励仅链接答案
        • substr() 函数通过指定字符串中字符的索引来工作,而不是根据我的需要指定子字符串!!
        • 也不会做所有需要的事情,strpos() 来帮助找到要分出的部分。
        猜你喜欢
        • 1970-01-01
        • 2013-02-26
        • 2014-12-29
        • 2020-11-17
        • 2018-11-14
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2017-02-20
        相关资源
        最近更新 更多