14-09-2006, 23:28
|
|
|
|
חבר מתאריך: 09.12.01
הודעות: 2,337
|
|
פונקציה למציאת כל הקישורים בעמוד מסויים
הייתי צריך לאיזה משהו פוקנציה להוצאת כל הקישורים בעמוד, פנימיים וחיצוניים,
וזה מה שיצא...
אם יש לכם שיפורים אשמח לראות,
הפונקציה מקבלת כתובת של קובץ (מקומי או חיצוני= http://www.domain.com/folder/myfile.asp) ומחזירה מערך עם שני תתי מערכים (ins ו outs; קישורים פנימיים וחיצוניים בהתאמה) שבכל אחד מהם רשימת מערכים (במידה ויש) של קישורים, בכל מערך יש תא text שזה מה שנקרא מבעצם ה href text, שיכול להיות גם תמונה וגם פיסקה שלמה, ו- href שזה הקישור עצמו (מה שקיים בתוך ה- href), במידה והקישור הוא פנימי (יש בדיקה מסויימת), ה-href בתוך מערך ה ins הוא רק המיקום היחסי שלו, ללא כתובת מלאה (ללא ה- http://www.mydomain.com/).
הפונקציה מנסה להימנע מכל מיני קישורי סרק כגון javascript:void() וכו'
הפונקציה:
קוד 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);
}
דומגה לפלט של הפונקציה על http://www.google.co.il :
קוד:
Array
(
[outs] => Array
(
[0] => Array
(
[text] => >קבוצות
[href] => http://groups.google.co.il/grphp?hl=iw&ie=UTF-8&tab=wg
)
[1] => Array
(
[text] => >חדשות<sup><span style="text-decoration:none;"><font color=red size="-2">חדש!</font></span></sup>
[href] => http://news.google.co.il/nwshp?hl=iw&ie=UTF-8&tab=wn
)
)
[ins] => Array
(
[0] => Array
(
[text] => >תמונות
[href] => imghp
)
[1] => Array
(
[text] => חיפוש מתקדם
[href] => advanced_search
)
[2] => Array
(
[text] => העדפות
[href] => preferences
)
[3] => Array
(
[text] => כלי שפה
[href] => language_tools
)
[4] => Array
(
[text] => >תכנית פרסום
[href] => intl/iw/ads
)
[5] => Array
(
[text] => הכל על Google
[href] => intl/iw/about.html
)
[6] => Array
(
[text] => דרושים
[href] => intl/iw/jobs/index.html
)
[7] => Array
(
[text] => Google.com in English
[href] => ncr
)
)
)
תהנו...
_____________________________________
“The man of wisdom is never in two minds about right and wrong;
the man of benevolence never worries about the future;
the man of courage is never afraid.”
~ Confucius
|