Jump to content

PHP code find


birdie

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
Link to comment
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]
Link to comment
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.
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.