![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Guest
Posts: n/a
|
What the fuck is wrong with my regex?
Code:
|<a\s[^>]*href\s*=\s*(\"??)'.$pData['profile_url'].'\\1[^>]*>'.$aData['link_backlink'].'<\/a> |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
|
Try this, assuming I understand the question
![]() Code:
<?php eregi('href="([^"]+)"[^>]*>([^<]+)', $page->source, $page->links); print_r($page->links); ?>
__________________
subarus. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
Guest
Posts: n/a
|
That doesn't work.
Pretty much it's looking for <a href="http://somesite.com">something</a> sounds easy, but it will also return true if theres other attributes in the a tag also. |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Too lazy to set a custom title
Industry Role:
Join Date: Aug 2002
Posts: 55,232
|
Code:
<? $link = '<a href="http://www.google.com" id="external">google</a>'; preg_match_all("/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU",$link,$matches); print_r($matches[2]); ?>
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence. ![]() WP Stuff |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 | |
Guest
Posts: n/a
|
Quote:
Thanks anyway.. Bump. |
|
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 |
So Fucking Banned
Industry Role:
Join Date: Feb 2006
Posts: 25,214
|
![]() ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 |
Confirmed User
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
|
Here is a way to do it without regex
Code:
$dom = new DOMDocument(); @$dom->loadHTML($html); $xpath = new DOMXPath($dom); $baseNodes = $xpath->evaluate("//base/@href"); if ($baseNodes->length == 1) { $baseUrl = rtrim($baseNodes->item(0)->nodeValue, '/'); } $hrefs = $xpath->evaluate("//a"); |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 |
Confirmed User
Join Date: May 2008
Posts: 3,406
|
PHP Code:
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 | |
Guest
Posts: n/a
|
Quote:
What I need it to do is find any a tag, with the specified href and anchor, ignores other attributes but returns true if the a tag has both the anchor and href |
|
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
Confirmed User
Join Date: Apr 2009
Posts: 1,319
|
I don't entirely understand what you are trying to do but maybe this will help... :shrug:
Code:
$links = '<a rel="nofollow" href="http://www.google.com/" id="extra">google</a>\r\n'; $links .= '<a rel="nofollow" href="http://www.yahoo.com/" id="extra">yahoo</a>\r\n'; $links .= '<a rel="nofollow" href=\'http://www.msn.com/\' id="extra">msn</a>\r\n'; $links .= '<a href="http://www.bing.com/" id="extra">bing</a>\r\n'; $links .= '<a href="http://www.ask.com/" id="extra">ask</a>\r\n'; $uri = 'www.bing.com'; $back = 'bing'; preg_match_all("/<a\s[^>]*href=([\"\']??)(http:\/\/{$uri}*?)([\"\']??)[^>]*>({$back})<\/a>/siU", $links, $matches); print_r( $matches );
__________________
History will be kind to me for I intend to write it. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#12 | ||
Confirmed User
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
|
Quote:
Quote:
Code:
<?php $url = 'http://www.crazyfilth.com'; $anchor_text = 'Crazy Porn'; $html = file_get_contents('http://www.filthdump.com'); echo checkUrl($url, $anchor_text, $html); function checkUrl($url, $anchor_text, $html) { $found = false; $dom = new domDocument(); @$dom->loadHTML($html); $anchors = $dom->getElementsByTagName('a'); foreach ($anchors as $anchor) { $found_url = $anchor->getAttribute('href'); $urltext = trim($anchor->nodeValue); if (($found_url == $url) && ($anchor_text == $urltext)) { return true; } } return false; } ?> |
||
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#13 |
Confirmed User
Join Date: Apr 2009
Posts: 1,319
|
I don't get that error but it would mean that a / was specified in the $uri
You could either put the http:// part in the regexp like I did, or replace all / with \/
__________________
History will be kind to me for I intend to write it. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#14 |
So Fucking Banned
Join Date: Aug 2003
Location: ICQ #23642053
Posts: 19,593
|
echo "I love google"; //for extra PR
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#15 |
Confirmed User
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#16 | |
Guest
Posts: n/a
|
Quote:
|
|
![]() ![]() ![]() ![]() ![]() |
![]() |
#17 | |
Confirmed User
Join Date: Apr 2009
Posts: 1,319
|
Quote:
Code:
$uri = 'http://www.bing.com/'; $uri = str_replace( '/', '\\/', $uri ); $back = 'bing'; preg_match_all("/<a\s[^>]*href=([\"\']??)({$uri}*?)([\"\']??)[^>]*>({$back})<\/a>/siU", $links, $matches); print_r( $matches );
__________________
History will be kind to me for I intend to write it. |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#18 |
Guest
Posts: n/a
|
Thanks for all the help, nation-x got me going with his function and it works perfectly.
|
![]() ![]() ![]() ![]() ![]() |
![]() |
#19 |
Confirmed User
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
|
Here is the final version for anyone that might need something similar... we found that there was a small issue with urls that had an ending slash... fixed.
Code:
<?php $url = 'http://www.crazyfilth.com'; $anchor_text = 'Porn Videos'; $html = file_get_contents('http://aisle69.com/'); echo checkUrl($url, $anchor_text, $html); function checkUrl($url, $anchor_text, $html) { $found = false; $dom = new domDocument(); @$dom->loadHTML($html); $anchors = $dom->getElementsByTagName('a'); foreach ($anchors as $anchor) { $found_url = preg_replace('{/$}', '', $anchor->getAttribute('href')); $urltext = trim($anchor->nodeValue); if (($found_url == $url) && ($anchor_text == $urltext)) { return true; } } return false; } ?> |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#20 |
Confirmed User
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
|
stupid gfy board :P double post
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#21 |
Confirmed User
Join Date: Apr 2009
Posts: 1,319
|
It's a good function for sure
![]() If the link has any extra info it isn't going to match. For example: Code:
$url = 'http://www.crazyfilth.com/'; $anchor_text = 'Porn Videos'; $html = '<a href="http://www.crazyfilth.com/?PHPSESSID=777" id="extra">Porn Videos</a>';
__________________
History will be kind to me for I intend to write it. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#22 | ||
Too lazy to set a custom title
Industry Role:
Join Date: Aug 2002
Posts: 55,232
|
Quote:
Code:
<?php $content = file_get_contents('test.html'); $regex = "/<a.*? href=(\"|')(.*?)(\"|').*?>(.*?)<\/a>/i"; if (preg_match_all($regex,$content,$matches,PREG_SET_ORDER)) { foreach ($matches as $match) { // echo $match[0]; // full link including href // echo $match[1]; // type of opening quote // echo $match[2]; // url // echo $match[3]; // type of closing quote // echo $match[4]; // link text } } ?> Quote:
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence. ![]() WP Stuff |
||
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#23 |
ICQ:649699063
Industry Role:
Join Date: Mar 2003
Posts: 27,763
|
This is too complicated for me.
Merry Christmas Happy New Year
__________________
Send me an email: [email protected] |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#24 |
Guest
Posts: n/a
|
Nice info Fris, Merry Christmas.
|
![]() ![]() ![]() ![]() ![]() |
![]() |
#25 |
Confirmed User
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
|
Good call on the follow up. I've since started using SIMPLE HTML DOM it's a PHP class that uses jQuery style selectors.
http://simplehtmldom.sourceforge.net/ So the original solution to this thread for accessing all anchor tag "hrefs" can be accomplished like this: Code:
// Create DOM from URL or file $html = file_get_html('http://www.google.com/'); // Find & print all link hrefs foreach($html->find('a') as $element) echo $element->href . '<br>';
__________________
subarus. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#26 |
Guest
Posts: n/a
|
Awesome reply Levi, Merry Christmas to you too man!
|
![]() ![]() ![]() ![]() ![]() |