【问题标题】:Get all the occurrences of a substring in a string获取字符串中所有出现的子字符串
【发布时间】:2012-09-14 15:50:18
【问题描述】:

我想做一件简单的事情:从字符串(即 HTML 文件)中提取代码的某些特定部分。

例如:

//Get a string from a website:
$homepage = file_get_contents('http://mywebsite.org');

//Then, search a particulare substring between two strings:
echo magic_substr($homepage, "<script language", "</script>");

//where magic_substr is this function (find in this awesome website):
function magic_substr($haystack, $start, $end) {

    $index_start = strpos($haystack, $start);
    $index_start = ($index_start === false) ? 0 : $index_start + strlen($start);

    $index_end = strpos($haystack, $end, $index_start); 
    $length = ($index_end === false) ? strlen($end) : $index_end - $index_start;

    return substr($haystack, $index_start, $length);
}

在这种情况下,我想要获得的输出是页面上的所有脚本。但是就我而言,我只能获得第一个脚本。我认为这是正确的,因为没有任何递归。但我不知道最好的方法是什么!有什么建议吗?

【问题讨论】:

  • 只要你不使用DOM Parser 在 html 文档中查找内容,小狗就会死得很惨。
  • 嗨,我尝试了 Simple Dom Parser,但遇到了“max_nested_level”问题......所以我以这种方式移动:)
  • max_nested_level 有什么问题?我相信 PHP Simple HTML Dom Parser 可以做到这一点。
  • 但是,请注意,DOM Parser 仅在 HTML 有效时才起作用。
  • 使用简单的 HTML Dom Parser 我达到了嵌套函数的限制,即 100,但我找不到如何更改这个值.. 我在这个网站上阅读了很多关于嵌套级别的内容,但我没有找到解决方案..所以我想以这种方式移动..我知道这有点难看:-D

标签: php substring


【解决方案1】:

试试这个从任何给定的标签或数据中提取数据 在你的情况下
extractor($homepage,"脚本语言,"脚本");
opps它没有正确显示脚本标签,但你定义你在你的例子中定义

/*****************************************************************/
/* string refine_str($str,$from,$to="")                         */
/* show data between $from and $to and also remove $from and $to */
/* if $to is not provided $from will be considered             */
/* a string to remove.                                           */
/*****************************************************************/

function extractor($str,$from,$to)
{
    $from_pos = strpos($str,$from);
    $from_pos = $from_pos + strlen($from);
    $to_pos   = strpos($str,$to,$from_pos);// to must be after from
    $return   = substr($str,$from_pos,$to_pos-$from_pos);
    unset($str,$from,$to,$from_pos,$to_pos );           
    return $return;

}    

【讨论】:

  • 与“my”函数相同:DI 只能看到 $from 字符串和 $to 字符串之间的第一个字符串。在我的情况下,必须有 19 个这种类型的匹配项。 . 我知道我要“解析”的特定文件的 html 结构,并且我确定字符串“from”和“to”总是相同的
  • 好的,我正在发布第二个答案,它将返回所有 occourense 的数组
  • 我发布了它,现在它在页面底部
【解决方案2】:
/****************************************************************/
/*  array getSelectiveContent($content,$from,$to,$exclude="")   */
/*  return array of content between provided                    */
/*  from and to positions.                                      */
/****************************************************************/

function getSelectiveContent($content,$from,$to,$exclude="")
{
    $return = array(); // array for return elements
    $size_FROM = strlen($from); 
    $size_TO = strlen($to);
while(true)
{
    $pos = strpos($content,$from); // find first occurance of $from
    if( $pos === false )
    {
        break;  // if not exist break loop
    }
    else
    {
        $element  = extractor($content,$from,$to); // fetch first element
        if($exclude == "")
        {
            if( trim($element) != "" )
            {
                $return[] = trim($element);
            }
        }
        else
        {
            if(trim($element) != "" && !strstr($element,$exclude)) // if nothing in range, and exclude is not in it
            {
                $return[] = trim($element); // put fetched content in array.
            }
        }
        $content = substr($content,$pos+strlen($element)+$size_FROM+$size_TO); // remove $from to $to from content 
    }
}
unset($content,$from,$to,$element,$exclude,$pos,$size_FROM,$size_TO);
return $return;
}

【讨论】:

    【解决方案3】:

    我喜欢从 dom-tree 获取元素的类似 Prototype/jQuery 的方式。

    试试jQuery-like interface for PHP。我没有在 PHP 中尝试过。

    编辑:

    对于有效的 HTML/XML,请尝试 TidyHTML PurifierhtmlLawled

    【讨论】:

      【解决方案4】:
      $text="this is an example of text extract in from very long long text this is my test of the php";
      $start="this";
      $end="of";
      $i=substr_count($text,$start);
      $k=substr_count($text,$end);
      $len1=strlen($start);
      $len2=strlen($end);
      $temp=$text;
      for ($j=1;$j<=$i;$j++){
              $pos1=strpos($temp,$start);
          $pos2=strpos($temp,$end);
          $subs=substr($temp,$pos1+$len1,$pos2-($pos1+$len1));
          echo $subs.'<br/>';
          $temp=substr($temp,$pos2+$len2,strlen($temp)-strlen($subs));
      }
      

      【讨论】:

      • 似乎可以“打印”输出中的任何内容:)
      猜你喜欢
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 2013-03-22
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多