0

私は簡単なことをしたい: 文字列 (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);
}

取得したい出力は、この場合、ページ上のすべてのスクリプトです。ただし、私の場合、最初のスクリプトしか取得できません。再帰がないので正しいと思います。しかし、これを行うための最良の方法がわかりません!助言がありますか?

4

4 に答える 4

1

これを試して、与えられたタグまたはデータからデータを抽出してくださいあなたの場合
、extractor($homepage,"script language,"script");
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;

}    
于 2012-09-22T12:49:55.233 に答える
1
/****************************************************************/
/*  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;
}
于 2012-09-22T13:02:50.577 に答える
0

dom-treeから要素を取得するPrototype/jQueryのような方法が好きです。

PHP用のjQueryのようなインターフェースからいくつか試してみてください。PHPでは試していません。

編集:

有効なHTML/XMLについては、 TidyまたはHTMLPurifier またはhtmlLawledを試してください。

于 2012-09-22T12:42:19.160 に答える
0
$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));
}
于 2012-09-22T13:12:16.693 に答える