Viewed   44 times

Possible Duplicate:
Grabbing the href attribute of an A element

I need to parse all links of an HTML document that contain some word (it's always different).

Example:

<a href="/bla:bla">BLA</a>
<a href="/link:link">BLA</a>
<a href="/link:bla">BLA</a>

I only need the links with "href=/link: ...." what's the best way to go for it?

$html = "SOME HTLM ";
$dom = new DomDocument();
@$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
foreach ($urls as $url)
{
    echo "<br> {$url->getAttribute('href')} , {$url->getAttribute('title')}";
    echo "<hr><br>";
}

In this example all links are shown, I need specific links.

 Answers

2

By using a condition.

<?php 
$lookfor='/link:';

foreach ($urls as $url){
    if(substr($url->getAttribute('href'),0,strlen($lookfor))==$lookfor){
        echo "<br> ".$url->getAttribute('href')." , ".$url->getAttribute('title');
        echo "<hr><br>";
    }
}
?>
Tuesday, November 8, 2022
1

FINAL ANSWER:

Okay, after checking out this thread, I've decided on this approach as the only one that seems to return an accurate measure:

$dt = DateTime::createFromFormat('Y-m-dTH:i:s.uZ', $date);
$now = new DateTime();
echo ($now->getTimestamp() - $dt->getTimestamp())."n";

ATTEMPTS:

In PHP 5.3 using classes (no 'U' format):

$dt = DateTime::createFromFormat('Y-m-dTH:i:s.uZ', $arr['launchTime']);
echo (new DateTime())->format('U');

In PHP 5.3 using procedural calls (also works like final solution):

$dt = date_create_from_format('Y-m-dTH:i:s.uZ', $arr['launchTime']);
$now = date_create();
echo ($now->getTimestamp() - $dt->getTimestamp());

In any version using strtotime (return wrong time):

date_default_timezone_set('UTC');
echo time() - strtotime($arr['launchTime']);
Tuesday, October 25, 2022
 
1

Actually include and require are identical in all except require will fail with E_ERROR while include will issue a warning. Also both of the statements are only activated when they actually executed inside script. So the following code will always work:

<?php
echo "Hello world";
if (0) require "non_existing.php";

The answer to your question is that index.php will be parsed first and executed. Then when include "init.php" encountered the file init.php is parsed and executed within current scope. The same for layout/header.php - it will be parsed first.

As already noted init.php will be parsed and executed each time include / require is called, so you probably will want to use include_once or require_once.

Friday, October 21, 2022
 
m8labs
 
3

To remove the line and print the output to standard out:

sed '/pattern to match/d' ./infile

To directly modify the file – does not work with BSD sed:

sed -i '/pattern to match/d' ./infile

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

sed -i '' '/pattern to match/d' ./infile

To directly modify the file (and create a backup) – works with BSD and GNU sed:

sed -i.bak '/pattern to match/d' ./infile
Friday, December 16, 2022
 
ivict
 
4

I suggest to use String+toLowerCase and check with String#indexOf, because it works in every browser.

if (gewaesser_name1.toLowerCase().indexOf("stream") === -1 && gewaesser_name2.toLowerCase().indexOf("stream") === -1) {
    var a = "..."
}
Friday, August 5, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :