Filter link uit a href html

Gestart doorRemcoboy4 reacties60 weergaven

5 berichten · pagina 1 van 1

Remcoboy

Lid · Topicstarter

Berichten
4.749
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Voor een script heb ik een filter nodig om de link uit een html code te halen. Hieronder een voorbeeld.
Ik kon hier op Google niet veel over vinden, hoe doe ik dit?

Input:
Lees verder

Output:
http://www.tipsvoorbesparen.nl/
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Regulair expression?
Preg_replace was dat geloof ik
0
Remcoboy

Lid · Topicstarter

Berichten
4.749
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Sevvlor schreef:
Regulair expression?
Preg_replace was dat geloof ik

Bedankt voor je tip. Op google wat gevonden hierover.

Heb nu de volgende code:
Spoiler tonen
[COLOR= #0000BB]<?php
$str[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]<a href="http://www.tipsvoorbesparen.nl/overig/mobiele-telefoon-sim-only-of-abonnement-met-toestel/">Lees verder</a>[/COLOR][COLOR= #007700];[/COLOR] 

[URL="http://www.php.net/preg_match_all"]preg_match_all[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]%(<a[^>](href=["|]{1}(.*)["|]{1})[^>]*>(.*?)</a>)%[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$str[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700]);[/COLOR] 

[URL="http://www.php.net/for"]for[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]<[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]]);[/COLOR] [COLOR= #0000BB]$a[/COLOR][COLOR= #007700]++)    {[/COLOR] 
    [COLOR= #0000BB]$sitename[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]4[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]];[/COLOR] 
    [COLOR= #0000BB]$url[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]3[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]];[/COLOR] 
    [COLOR= #0000BB]$new[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$url[/COLOR][COLOR= #007700];[/COLOR] 
     
    [COLOR= #0000BB]$str[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/preg_replace"]preg_replace[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]%[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]%[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$new[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$str[/COLOR][COLOR= #007700]); 

}[/COLOR] 

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]RESULT:<br>[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$str[/COLOR][COLOR= #007700];[/COLOR] 
?>
Deze maakt van Lees verder netjes http://www.tipsvoorbesparen.nl/.

Hoe koppel ik $str aan $result?

$query = mysql_query("SELECT tip_text FROM tvb_dailytipdata WHERE shown_date = 2013-02-03");
        $result = mysql_fetch_assoc($query);


toevoeging op 3 februari 2013, 131839:
Als ik op de plek van $str in de bovenste code $result invoer krijg ik:
Warning: preg_match_all() expects parameter 2 to be string, array given in * on line 27
0
Berichten
5.274
Reputatie
1
Punten
0,01
Lid sinds
Laatst gezien

Eindhoven, Nederland

Hele simpele fix:

[COLOR= #0000BB]<?php
$query[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT tip_text FROM tvb_dailytipdata WHERE shown_date = 2013-02-03"[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #0000BB]$str[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$result[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]tip_text[/COLOR][COLOR= #007700]];[/COLOR]

[URL="http://www.php.net/preg_match_all"]preg_match_all[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]%(<a[^>](href=["|]{1}(.*)["|]{1})[^>]*>(.*?)</a>)%[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$str[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700]);[/COLOR] 

[URL="http://www.php.net/for"]for[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]<[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]]);[/COLOR] [COLOR= #0000BB]$a[/COLOR][COLOR= #007700]++)    {[/COLOR] 
    [COLOR= #0000BB]$sitename[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]4[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]];[/COLOR] 
    [COLOR= #0000BB]$url[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]3[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]];[/COLOR] 
    [COLOR= #0000BB]$new[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$url[/COLOR][COLOR= #007700];[/COLOR] 
     
    [COLOR= #0000BB]$str[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/preg_replace"]preg_replace[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]%[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]%[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$new[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$str[/COLOR][COLOR= #007700]); 

}[/COLOR] 

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]RESULT:<br>[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$str[/COLOR][COLOR= #007700];[/COLOR]
0
Remcoboy

Lid · Topicstarter

Berichten
4.749
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Wouterr schreef:
Hele simpele fix:
[COLOR= #0000BB]<?php
$query[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT tip_text FROM tvb_dailytipdata WHERE shown_date = 2013-02-03"[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #0000BB]$str[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$result[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]tip_text[/COLOR][COLOR= #007700]];[/COLOR]

[URL="http://www.php.net/preg_match_all"]preg_match_all[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]%(<a[^>](href=["|]{1}(.*)["|]{1})[^>]*>(.*?)</a>)%[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$str[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700]);[/COLOR] 

[URL="http://www.php.net/for"]for[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]<[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]]);[/COLOR] [COLOR= #0000BB]$a[/COLOR][COLOR= #007700]++)    {[/COLOR] 
    [COLOR= #0000BB]$sitename[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]4[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]];[/COLOR] 
    [COLOR= #0000BB]$url[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]3[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]];[/COLOR] 
    [COLOR= #0000BB]$new[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$url[/COLOR][COLOR= #007700];[/COLOR] 
     
    [COLOR= #0000BB]$str[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/preg_replace"]preg_replace[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]%[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$matches[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]%[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$new[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$str[/COLOR][COLOR= #007700]); 

}[/COLOR] 

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]RESULT:<br>[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$str[/COLOR][COLOR= #007700];[/COLOR]


Helemaal top!
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren