Current location: Hot Scripts Forums » Programming Languages » PHP » Need help with preg_match_all


Need help with preg_match_all

Reply
  #1 (permalink)  
Old 10-28-06, 05:24 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Need help with preg_match_all

PHP Code:

$preg '#<a.*?href="(.*?)".*?>(.*?)</a>#i';

if(
preg_match_all($preg$tmp$results)) {
foreach(
$results[1] as $index => $href) {
$content $results[2][$index];
if ( (
htmlentities(strip_tags($content))) == 'Visit' ) {
echo 
$href."<br>";
}
}

I use this code to get all link from a html file >>> Choose links called "Visit" >>> Output ( $results[1][$index] or $href ). There is 3 results and I just want to select only one of them ( eg : the second ) and I used $href[2] but I wont work. So can any one plz give me an advice ?
Reply With Quote
  #2 (permalink)  
Old 10-28-06, 05:36 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Could you post examples of the links? I'm not sure if I understand what you're trying to do.
Reply With Quote
  #3 (permalink)  
Old 10-28-06, 09:09 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico
Could you post examples of the links? I'm not sure if I understand what you're trying to do.
Here is an example :
Code:
http://vmyth.info/test.php
The code of the HTML :
Code:
<a href="google.com">Visit</a>
<a href="yahoo.com">Visit</a>
<a href="msn.com">Visit</a>
The script gets all the links called "Visit" ( google.com, yahoo.com, msn.com etc ) and display them. But I only want to select one of the result ( eg : yahoo.com or msn.com only ). How do I do this ?
Reply With Quote
  #4 (permalink)  
Old 10-28-06, 09:15 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You could do something like that.
PHP Code:

preg_match_all('/<a(.*?)href=\"(.*?)\"(.*?)>Visit<\/a>/si'$text$matches);



$matches[2][0]; // Google
$matches[2][1]; // Yahoo
$matches[2][2]; // MSN 
Reply With Quote
  #5 (permalink)  
Old 10-28-06, 09:31 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico
You could do something like that.
PHP Code:

preg_match_all('/<a(.*?)href=\"(.*?)\"(.*?)>Visit<\/a>/si'$text$matches);



$matches[2][0]; // Google
$matches[2][1]; // Yahoo
$matches[2][2]; // MSN 
Thank you very much but can you give me the full code ?
Reply With Quote
  #6 (permalink)  
Old 10-28-06, 09:49 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Well, this is basically the whole code? What do you want to do? Randomly pick one of there links and show them, or a specific one?
Reply With Quote
  #7 (permalink)  
Old 10-28-06, 09:51 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Oh I'm sorry. I have already got it. Thank you again.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 05:44 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.