verwijder alle links

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Luc Gomes

Luc Gomes

26/08/2014 13:33:36
Quote Anchor link
Hallo,
Ik heb het onderstaande script om gegevens op te halen, nu wil ik dat alle links in een keer worden verwijderd.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$regx
= '/<font size="1">(.*)<\/font>?/msU';
$scrape_address = "http://www.biljartpoint.nl/index.php?page=uitslagdetail&district=57&progid=246515&f=1&poule=A&compid=1022";
$ch = curl_init($scrape_address);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
preg_match($regx, $data, $match);
strip_tags($match, '<br>');
$match[1] = str_replace('<a href="index.php?page=pr&bondsnr=139366&klasse=B1&seizoen=2014-2015&d=57">', ' ', $match[1]);

echo $match[1];
?>

Kan iemand mij helpen?
 
PHP hulp

PHP hulp

28/03/2024 19:55:34
 
Peter  paul

peter paul

01/09/2014 15:51:16
Quote Anchor link
Als ik het goed begrijp wil je dat de namen geen links meer zijn, kijk een naar preg-replace().

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
<?php
$regx
= '/<font size="1">(.*)<\/font>?/msU';
$scrape_address = "http://www.biljartpoint.nl/index.php?page=uitslagdetail&district=57&progid=246515&f=1&poule=A&compid=1022";
$ch = curl_init($scrape_address);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
preg_match($regx, $data, $match);

echo preg_replace('/<a href="(.*?)">/', '', $match[1]);
?>
 
Ivo P

Ivo P

01/09/2014 16:16:16
Quote Anchor link
'misschien ook de afsluitende </a> meepakken bij de replace?
 
Wouter J

Wouter J

01/09/2014 16:27:02
Quote Anchor link
Nooit HTML parsen met Regexen, HTML is te complex om dat met regexen te kunnen. Wat denk je bijv. van newlines in je tags, van eventuele attributen, de optionele sluittags (bij sommige elementen), etc?

Om links te verwijderen zul je een HTML parser moeten gebruiken. Bijv. die van PHP:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$content
= '';

$dom = new \DOMDocument();
$dom->loadHTML($content);

$as = array();
foreach ($dom->getElementsByTagName('a') as $a) {
    $as[] = $a;
}


foreach ($as as $a) {
    $a->parentNode->removeChild($a);
}

// we moeten het in 2 loopen doen, omdat de parser anders de weg kwijt raakt

$textWithoutLinks = $dom->saveHtml();
?>
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.