I want to include an external file, and then get all the content, remove the only HTML on the page <br />
and replace with ,
and fire it into an array.
datafeed.php
john_23<br />
john_5<br />
john_23<br />
john_5<br />
grabber.php
<?php
// grab the url
include("http://site.com/datafeed.php");
//$lines = file('http://site.com/datafeed.php);
// loop through array, show HTML source as HTML source.
foreach ($lines as $line_num => $line) {
// replace the <br /> with a ,
$removeBreak = str_replace("<br />",",", $removeBreak);
$removeBreak = htmlspecialchars($line);
// echo $removeBreak;
}
// fill our array into string called SaleList
$SaleList = ->array("");
I want to load a php file from the server directory, get the HTML contents of this file and place it into a useable array.
It would look like
$SaleList = -getAndCreateArray from the file above >array("");
$SaleList = ->array("john_23, john_5, john_23");
Here is a working version of grabber.php
:
<?php
function getSaleList($file) {
$saleList = array();
$handle = fopen($file, 'r');
if (!$handle) {
throw new RuntimeException('Unable to open ' . $file);
}
while (($line = fgets($handle)) !== false) {
$matches = array();
if (preg_match('/^(.*?)(\s*\<br\s*\/?\>\s*)?$/i', $line, $matches)) {
$line = $matches[1];
}
array_push($saleList, htmlspecialchars($line));
}
if (!feof($handle)) {
throw new RuntimeException('unexpected fgets() fail on file ' . $file);
}
fclose($handle);
return $saleList;
}
$saleList = getSaleList('datafeed.php');
print_r($saleList);
?>
By using a regular expression to find the <br />
, the code is able to deal with many variations such as <br>
, <BR>
, <BR />
, <br/>
, etc.
The output is:
Array
(
[0] => john_23
[1] => john_5
[2] => john_23
[3] => john_5
)
May you need something like this?
$file = file_get_contents('newfile.php');
echo str_replace("<br>", ",", $file);
But I didn't get what you try to insert into the array...
You don't seem to have grasped what include does.
If you want to process the contents of some file using your PHP code, then include is the wrong construct - you should be using file() or file_get_contents().
i.e. using the line of code you've commented out in your question.
Where include is the right construct to use....you should never, ever include remote files directly - its much MUCH slower than a local filesystem read - and very insecure. There are times when it does make sense to fetch the file from a remote location and cache it locally though.
And you should NEVER have inline HTML nor PHP code (html in PHP variables/conditional expressions, and PHP defines/class/function/include/require are OK) in an include file.