我正在通过这个循环获取页面的所有ahref:
foreach($html->find('a[href!="#"]') as $ahref) {
$ahrefs++;
}
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
foreach($html->find('a[href!="#"]') as $ahref) {
if(isexternal($ahref)) {
$external++;
}
$ahrefs++;
}
Run Code Online (Sandbox Code Playgroud)
外在的地方是一个功能
function isexternal($url) {
// FOO...
// Test if link is internal/external
if(/*condition is true*/) {
return true;
}
else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
救命!
Rus*_*Bes 15
使用parse_url并将主机与本地主机进行比较(通常但并不总是与之相同$_SERVER['HTTP_HOST'])
function isexternal($url) {
$components = parse_url($url);
return !empty($components['host']) && strcasecmp($components['host'], 'example.com'); // empty host will indicate url like '/relative.php'
}
Run Code Online (Sandbox Code Playgroud)
Hovewer这将把www.example.com和example.com视为不同的主机.如果您希望将所有子域都视为本地链接,那么该函数将会更大一些:
function isexternal($url) {
$components = parse_url($url);
if ( empty($components['host']) ) return false; // we will treat url like '/relative.php' as relative
if ( strcasecmp($components['host'], 'example.com') === 0 ) return false; // url host looks exactly like the local host
return strrpos(strtolower($components['host']), '.example.com') !== strlen($components['host']) - strlen('.example.com'); // check if the url host is a subdomain
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14947 次 |
| 最近记录: |