Modernvox Posted January 2, 2010 Share Posted January 2, 2010 Happy New Year Guyz Hope everyone made it out okay I have this code which returns CL Postings pertaining to Musicians. The only problems I see are: 1. It shows array[0] being the data i need, but in the script it shows array/Post[1] being printed which is confusing me? Is it Array[0] or Array[1] being displayed first in the script? Also, I need the link to remain intact, that is, linking back to CL. So here's what the output is that I need which are actually links: However I DO NOT want the Array syntax included i.e. [0] =>, [1] => etc.. [0] => Female Singer needed - (providence) [1] => JANUARY 8th, FRIDAY NIGHT - ROCK / PUNK / PSYCHOBILLY - (New Bedford) [2] => Looking for a musician who is very open minded?.. - (New Bedford / Rochester) [3] => DRUMMER/VOCALS - (SE MASS) [4] => Gig for gig... - (Boston) [5] => BASS AVAILABLE For Bluesy,Rootsy,Classic,Hard Rock. - (Cape / Boston / RI) [6] => Songwriters ????? - (south coast) [7] => New Years Eve Party - (Belmont Club Fall River) [8] => we need a frontman and bassist - (new bedford ma) [9] => LOOKING FOR BASS PLAYER - (Taunton, MA) [10] => Band Ready to Gig Seeks Bassist - (Southcoast,MA) [11] => Progressive Psychedelic Rock Band Auditioning Drummers - (Plymouth) [12] => DRUM LESSONS>>>>>>>with Mike Levesque - (S.E. MA) [13] => 4 BAD MOTHERFUCKER shows this wek - (new bedford) [14] => Singing in the bedroom..."Start" the Jam - (Stuck in the 80's) Here is my code: <?php $html = file_get_contents("http://southcoast.craigslist.org/muc/"); preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts); echo "<pre>";print_r($posts); foreach ($posts as $post) { print $post[1]; } ?> Any help greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/186972-preserving-original-links-in-an-array/ Share on other sites More sharing options...
MadTechie Posted January 2, 2010 Share Posted January 2, 2010 You probably want the Set Order for example <?php $html = file_get_contents("http://southcoast.craigslist.org/muc/"); preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts,PREG_SET_ORDER); //echo "<pre>";print_r($posts); foreach ($posts as $post) { //print $post[0]; //HTML print $post[1]; //Link print $post[2]; //Description print $post[3]; //Short Text print "<BR />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186972-preserving-original-links-in-an-array/#findComment-987353 Share on other sites More sharing options...
Modernvox Posted January 3, 2010 Author Share Posted January 3, 2010 You probably want the Set Order for example <?php $html = file_get_contents("http://southcoast.craigslist.org/muc/"); preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts,PREG_SET_ORDER); //echo "<pre>";print_r($posts); foreach ($posts as $post) { //print $post[0]; //HTML print $post[1]; //Link print $post[2]; //Description print $post[3]; //Short Text print "<BR />\n"; } ?> Displays the data, but without working link displays as: /muc/1534468295.htmlLookin for a rockin singer for working band - (SE Mass) /muc/1533839037.htmlFemale Singer needed - (providence) /muc/1533816253.htmlJANUARY 8th, FRIDAY NIGHT - ROCK / PUNK / PSYCHOBILLY - (New Bedford) Notice the muc/1534468295.html in front? This shouldn't be showing there.. Quote Link to comment https://forums.phpfreaks.com/topic/186972-preserving-original-links-in-an-array/#findComment-987384 Share on other sites More sharing options...
MadTechie Posted January 3, 2010 Share Posted January 3, 2010 Well it was an example for the order of the array!, format the text however you wish, hence the comments stating whats what, or just use the HTML line (i commented it out for ease of use) Quote Link to comment https://forums.phpfreaks.com/topic/186972-preserving-original-links-in-an-array/#findComment-987390 Share on other sites More sharing options...
Modernvox Posted January 3, 2010 Author Share Posted January 3, 2010 Well it was an example for the order of the array!, format the text however you wish, hence the comments stating whats what, or just use the HTML line (i commented it out for ease of use) Understood MT , I have the text/data in check , but why can't I get the link to be active? It just keeps pointing to localhost or wherever else I run the script. I want it to open in it's original location which is craigslist Quote Link to comment https://forums.phpfreaks.com/topic/186972-preserving-original-links-in-an-array/#findComment-987393 Share on other sites More sharing options...
MadTechie Posted January 3, 2010 Share Posted January 3, 2010 if you have a link without the domain at the start it will append to the current domain, so for example <a href="/login.php">login</a> will go to the login.php of the current domain (here would be http://www.phpfreaks.com/login.php, on google.com it would be google.com/login.php etc etc this might work (may need some tweaking) <?php $url = "http://southcoast.craigslist.org"; $html = file_get_contents("$url/muc/"); preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts,PREG_SET_ORDER); //echo "<pre>";print_r($posts); foreach ($posts as $post) { //print $post[0]; //HTML $post[2] = str_ireplace($url,"",$post[2]); //remove domain echo "<a href=\"$url/{$post[1]}\">{$post[2]}<\/a><font size=\"-1\">{$post[3]}<\/font>"; print "<BR />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186972-preserving-original-links-in-an-array/#findComment-987394 Share on other sites More sharing options...
Modernvox Posted January 3, 2010 Author Share Posted January 3, 2010 if you have a link without the domain at the start it will append to the current domain, so for example <a href="/login.php">login</a> will go to the login.php of the current domain (here would be http://www.phpfreaks.com/login.php, on google.com it would be google.com/login.php etc etc this might work (may need some tweaking) <?php $url = "http://southcoast.craigslist.org"; $html = file_get_contents("$url/muc/"); preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts,PREG_SET_ORDER); //echo "<pre>";print_r($posts); foreach ($posts as $post) { //print $post[0]; //HTML $post[2] = str_ireplace($url,"",$post[2]); //remove domain echo "<a href=\"$url/{$post[1]}\">{$post[2]}<\/a><font size=\"-1\">{$post[3]}<\/font>"; print "<BR />\n"; } ?> Yep, Took out the extra / after echo "<a href=\"$url/{$post[1]}\and it works as expected. Thank You Quote Link to comment https://forums.phpfreaks.com/topic/186972-preserving-original-links-in-an-array/#findComment-987400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.