Grab text from another page?

SpaceshipSpaceship
NamecheapNamecheap
Namecheap AuctionsNamecheap Auctions
Watch

simonj

Established Member
Impact
15
Is it possible to grab text from another site and put it as the default value in a textbox? ie. I want to get the text from http://www.theage.com.au/text/ and put it in a textbox on my site. I don't want the links, just the actual text. Is this possible in PHP?

thanks
Simon
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Yes it is possible. Here are the steps (your PHP must support curl).
  1. Use PHP curl to grab the html content of http://www.theage.com.au/text/
  2. Because you only want to read the text, not the whole html content (including the links), you must remove those links manually by PHP regular expression.
  3. After you have gotten the 'clean' content, you can use it to your textbox, etc.
 
0
Close
Um, ok thanks. I guess I'll google Curl then.
 
0
Close
Here is a simplied example that will fetch the page, return the text between <body> tags, strip HTML tags and apply line breaks for display.
PHP:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.theage.com.au/text/");
curl_setopt($ch, CURLOPT_HEADER, 0);
$html=curl_exec($ch); // fetch URL
curl_close($ch);

preg_match("/<body.*>(.*)<\/body>/smU",$html, $results);
$text=$results[1];
$text=strip_tags($text); // remove HTML tags
?>
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
</head>
<body>
<?php
echo '<fieldset><legend>Retrieved text</legend>'.nl2br(htmlspecialchars($text)).'</fieldset>';
?>
</body>
</html>
 
1
Close
that's awesome, thanks!


Can all that be reduced to a single variable that I can put in the Value= of a textbox?
 
Last edited:
0
Close
Based on the previous code, the content is stored in $text.
You can put it in your textbox : :)
PHP:
<input type="text" name="something" value="<?php echo htmlentities($text); ?>" size="50" />
 
1
Close
I see, thanks!
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

New: watch any domain
You can now watch domains on NamePros. We check each one and notify you when something changes. Search for a domain and press Watch on...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back