הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   פורום תיכנות (https://hosts.co.il/forums/forumdisplay.php?f=14)
-   -   עזרה| קוד שלא פועל (https://hosts.co.il/forums/showthread.php?t=71762)

possible 05-02-09 21:03

עזרה| קוד שלא פועל
 
הקוד אמור לקבל את כל הקוד מקור של דף מסויים ואז הוא עובר על כל הקוד ברקורסייה ומציג את כל הקישורים שנמצאים בו.

PHP קוד:

<?php
$source 
file_get_contents("http://programart.co.cc/");
$linkpos1 strpos($source"<a ");

function 
getLinks($linkpos)
{
    
$targetpos=0;
    
$endtarget=0;
    if (
$linkpos!==false)
    {
        
$targetpos strpos($source"href=".'"',$linkpos+3);
        
$endtarget strpos($source'"',$targetpos+6);
        
$link substr($source$targetpos+6$endtarget-1); 
        echo 
$link."<br />"$targetpos+."<br />".$endtarget-1;
        
$linkpos strpos($source"<a ",$linkpos);
        
getLinks($linkpos);
    }
    else
    {    
        echo 
"Error";
    }    
}
getLinks($linkpos1+3);
echo 
$linkpos1;
?>

במקום לבצע את מה שרציתי הוא מציג את השגיאה:
Warning: strpos() [function.strpos]: Offset not contained in string in /home/devsys/domains/eyesharp.co.cc/public_html/check.php on line 11

Warning: strpos() [function.strpos]: Offset not contained in string in /home/devsys/domains/eyesharp.co.cc/public_html/check.php on line 12
5
Warning: strpos() [function.strpos]: Offset not contained in string in /home/devsys/domains/eyesharp.co.cc/public_html/check.php on line 15

ואני לא מבין למה הוא אומר לי שהארוך של הטקסט לא ארוך מספיק לאורך שהבאתי לו :O

תודה לעוזרים.

נ.ב הוא גם רושם Error בסוף.

vadimg88 05-02-09 21:26

אתה יכול לקבל את כל הקישורים מהעמוד בעזרת preg_match_all לא צריך פונקציה רקורסיבית.

Shon12 05-02-09 22:11

^^כמו שוואדים אמר-preg_match_all:
PHP קוד:

<?php
$source 
file_get_contents("http://lasw.gamli.net/s.html"); 

    
preg_match_all("/\<a.+>(.+?)<\/a>/i",$source,$match);
    for (
$i 0$i <= strlen($match[0]); $i++)
    {
    echo 
$match[0][$i]."<br />";
    }
?>


possible 05-02-09 22:12

תודה רבה פועל.
אבל זה עדיין לא מציג לי את כל הקישורים שיש באותו עמוד.
נגיד מתי שהוא מציג של האתר programart.co.cc
הוא מציג רק את הקישורים האלה:
http://www.eyesharp.co.cc/check.php

אשמח גם להסבר קל איך הפונקצייה הזאת פועלת ^^
תודה רבה לכם

Shon12 05-02-09 22:32

PHP קוד:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<?php
$source 
file_get_contents("http://programart.co.cc"); 

    
preg_match_all("/\<a.+>(.+?)<\/a>/i",$source,$match);
    foreach(
$match[0] as $value)
    {
    echo 
$value."<br />";
    }
?>

דוגמא:
http://lasw.gamli.net/s.php
http://il.php.net/preg_match_all

תהנה :)
ואין בעד מה :)

Elad-A 05-02-09 22:58

יש פונקצייה שנכתבה ע"י The AcE :

PHP קוד:

function getLinks ($file) {
    
$ur=parse_url($file);            #parsh the URL
    
$data=file_get_contents($file);    #get the file from the server
    
$regex="/<a.*href=[\"']?([^\"' >]+)[\"' >](.*)<\/a>/imsU";
    
preg_match_all($regex,$data,$matches,PREG_SET_ORDE  R);
    
$_ins=array(); $_outs=array();
    
$_ins[]=trim($ur['path'],"/");
    foreach (
$matches as $match) {
        
$purl=parse_url($match[1]);
        if ((    
#check if it is an inner link
                
($purl['host']    == $ur['host'] and $purl['scheme'] == 'http')
            or    (!
$purl['host'] and !$purl['scheme']))        #check if has host (for when href is: 'javascript' then scheme is 'javascript' and no host set)
            
and    !in_array(trim($purl['path'],"/"),$_ins)    #check if not in our array, we don't need duplicated addresses
            
) {
            
$ins[]=array('text'=>$match[2],'href'=>trim($purl['path'],"/"));
            
$_ins[]=trim($purl['path'],"/");
            continue;
        } elseif (
                
$purl['host'] != $ur['host']    #check that it is actually an outgoing link
            
and    !in_array(ltrim($match[1],"/"),$_outs#check if not in our array to avoid duplicated links
            
and    $purl['scheme'] == "http"         #get only the http links
            
) {
            echo 
"Putting in outs.\n";
            
$outs[]=array('text'=>$match[2],'href'=>ltrim($match[1],"/"));
            
$_outs[]=ltrim($match[1],"/");
            continue;
        }
    }
    return array(
'outs'=>$outs,'ins'=>$ins);


הפונקצייה מחזירה פלט בצורה של קישורים נכנסים \ יוצאים מהדף, כמובן שכל הקרדיט הולך אליו כן?


כל הזמנים הם GMT +2. הזמן כעת הוא 12:35.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ