NameSilo

Grab text from another page?

SpaceshipSpaceship
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
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains โ€” AI StorefrontUnstoppable Domains โ€” AI Storefront
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
•••
Um, ok thanks. I guess I'll google Curl then.
 
0
•••
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
•••
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
•••
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
•••
I see, thanks!
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Spaceship
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back