Jump to content

Archived

This topic is now archived and is closed to further replies.

birdie

PHP code find

Recommended Posts

ok, i'm not completely sure how complicated this is but it is in my head.

say i wanted to find a hyperlink url out of a normal php code

The code that i would be designating would be something like..

[code]
<a href="thispage.php">Click here</a>
[/code]
How could i find "thispage.php"? ive tryed sub_str etc but i cant find a way to make them useful. any help? thanks

Share this post


Link to post
Share on other sites
Not exactly sure what you're looking for.
Are you trying to extract the url from the html code?
In that case, you could try something like that below.
It uses regular expressions - though I must disclose I am
far from being an expert in the use of regular expressions.

[code]<?php
$html_code = '<a href="thispage.php">Click Here</a>';
$pattern = '/(.*)(<a href=")(.*)(">)(.*)/';
preg_match($pattern, $html_code, $matches);
echo $matches[3];
?>[/code]

Share this post


Link to post
Share on other sites
Thanks i think thats what i'm looking for. :-)

Share this post


Link to post
Share on other sites
[!--quoteo(post=350978:date=Mar 2 2006, 10:10 AM:name=KHB)--][div class=\'quotetop\']QUOTE(KHB @ Mar 2 2006, 10:10 AM) [snapback]350978[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Not exactly sure what you're looking for.
Are you trying to extract the url from the html code?
In that case, you could try something like that below.
It uses regular expressions - though I must disclose I am
far from being an expert in the use of regular expressions.

[code]<?php
$html_code = '<a href="thispage.php">Click Here</a>';
$pattern = '/(.*)(<a href=")(.*)(">)(.*)/';
preg_match($pattern, $html_code, $matches);
echo $matches[3];
?>[/code]
[/quote]

great suggestion, but let me clean it up a tad... as it is, there are a lot of things that [i]could[/i] be in the source that may throw off your matches. i'd try to change it up just a tad:
[code]
$code = "<a href='http://www.cnn.com/'>CNN</a><br /><a href=\"google.com\">Google</a>\n";

preg_match_all("|href=([\"'])(.+?)\\1|i", $code, $matches, PREG_PATTERN_ORDER);
echo "URLs in this code:<br />\n";
foreach ($matches[2] as $match) {
  echo "$match<br />\n";
}
[/code]

this way, you account for hrefs within double OR single quotes, too... as well, you are accounting for the possibility of having multiple URLs in one string.

Share this post


Link to post
Share on other sites
[!--quoteo(post=351005:date=Mar 2 2006, 04:29 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Mar 2 2006, 04:29 PM) [snapback]351005[/snapback][/div][div class=\'quotemain\'][!--quotec--]
great suggestion, but let me clean it up a tad... as it is, there are a lot of things that [i]could[/i] be in the source that may throw off your matches. i'd try to change it up just a tad:
[code]
$code = "<a href='http://www.cnn.com/'>CNN</a><br /><a href=\"google.com\">Google</a>\n";

preg_match_all("|href=([\"'])(.+?)\\1|i", $code, $matches, PREG_PATTERN_ORDER);
echo "URLs in this code:<br />\n";
foreach ($matches[2] as $match) {
  echo "$match<br />\n";
}
[/code]

this way, you account for hrefs within double OR single quotes, too... as well, you are accounting for the possibility of having multiple URLs in one string.
[/quote]
thanks thats even better!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.