Jump to content

Array returning weird values


Modernvox

Recommended Posts

Hi guys, I have this code, but it is not returning the values as I had planned.

It shows the title as a local link only and doesn't show more than 2 posts at a time.

 

Here is the output i am getting:

Violin and Voice Lessons - (ri)Small Band Practice Space - (Olneyville)/muc/1530263612.html/muc/1530038374.htmlViolin and Voice Lessons -Small Band Practice Space - (ri) (Olneyville)

 

Here is the expected output:<(note: titles should be a link like on Craigslist.

D R U M_L E S S O N S>>>>with mike levesque - (RI/MA)

 

Violin and Voice Lessons - (ri)

 

Godsmack tribute"moonbaby" seeks sully - (ri)

 

Huge Band Practice Space - (Olneyville)

 

Small Band Practice Space - (Olneyville)

 

  <?php
$html = file_get_contents("http://southcoast.craigslist.org/muc/");

preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)

<\/font>/', $html,$posts);

foreach ($posts as $post)
{
    echo $post[1];
    $title = $post[2];
   $date = $post[3];
   echo  $content = $post[4];
  
// do something with data
}
?> 

Link to comment
https://forums.phpfreaks.com/topic/186711-array-returning-weird-values/
Share on other sites

Displayed like such:

Band needed for NY's Eve - (RI)Violin and Voice Lessons - (ri)/muc/1530332391.html/muc/1530263612.htmlBand needed for NY's Eve -Violin and Voice Lessons - (RI) (ri)

 

It's just not displaying properly. Should be displaying in similar format as craigslist itself http://providence.craigslist.org/muc/

I have it displaying:

Band needed for NY's Eve - (RI)/muc/1530332391.htmlBand needed for NY's Eve - (RI)

 

Notice it is being repeated?

/muc/1530332391.htmlBand needed for NY's Eve - (RI) 

I need to get rid of the extra post.

 

Also it is only displaying the first one. I need at least 30 to display. Finally, the link is suppose to link back to craigslist , but it links to my localhost?

not to sure how you want the content but I came up with this

 

<?php
$html = file_get_contents("http://southcoast.craigslist.org/muc/");

preg_match_all('/<h4>(.*?)<\/h4>|<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts);

$content = array();

$cur_date = '';
for ($i=0;$i < count($posts[0]);$i++)
{
   if (date("D M d", strtotime($posts[1][$i])) == $posts[1][$i]) { 
        $cur_date = $posts[1][$i];
        continue;
   }
   $content[] = array('date' => $cur_date, 
       'href' => $posts[2][$i],
   'content' => $posts[3][$i],
   'location' => $posts[4][$i]);
  
// do something with data
}

print_r($content);
?>

Archived

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

×
×
  • 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.