TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Pulling values from one site as reference (http://www.talkphp.com/absolute-beginners/3980-pulling-values-one-site-reference.html)

9three 02-17-2009 12:15 AM

Pulling values from one site as reference
 
Hey,

I'm trying to figure out if its possible to pull a value(s) from one site (site a) and update my site (site b) with whatever those values are?

What function is available in PHP for this, if any?

Wildhoney 02-17-2009 12:31 AM

How about using regular expressions? For instance, you could use this little bit of code to get the latest 5 articles from TalkPHP, as displayed on the left of the website:

php Code:
$szContent = file_get_contents('http://www.talkphp.com/forums.php');
preg_match_all('~<td class="title" valign="top">(.+?)</td>~is', $szContent, $aMatches);

foreach ($aMatches[0] as $szArticle)
{
    printf("- %s<br />\n", $szArticle);
}

9three 02-17-2009 05:38 AM

hey thanks, I'm stil learning regular expressions. Could you tell me what (.+?) does please?

Edit:

Also, I'm pulling an image from a site but it's only adding the <img src="/path/to/image.jpg"> and so the image doesn't display. I need to manually add the URL infront of it. I'm not sure how to do this within the regular expression.

Wildhoney 02-17-2009 02:12 PM

The .+? says any character but white space, and find all those characters, but in non-greedy form, so when it finds the less than symbol "<" it will stop capturing.

For the appending of the URL to the image path, you could try something like the following:

php Code:
$aImages = array('/path/to/image.jpg', '/path/to/image.gif');
$szFunction = create_function('$s', 'return "http://www.talkphp.com" . $s;');
$aImages = array_map($szFunction, $aImages);
die(print_r($aImages, true));

Salathe 02-17-2009 02:37 PM

Quote:

Originally Posted by Wildhoney (Post 21835)
The .+? says any character but white space

Not strictly true. By default, . will match any character which isn't the newline character (so the same as [^\n]): any other whitespace character (like space, tab, carriage return) will be matched. With the s (PCRE_DOTALL) modifier, the dot will match any character (including the newline).


All times are GMT. The time now is 05:46 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0