0

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");
12
  • include() on remote urls is dangerous. You're giving an external server COMPLETE control of your script, and therefore complete control of your website. Beyond that... is there a question there? You don't mention having a problem, so if this code is working fine, then why post it? Commented Nov 24, 2011 at 14:53
  • include is the only file operator you know? what about file(), file_get_contents()? Commented Nov 24, 2011 at 14:54
  • @MarcB this code doesn't work though. Commented Nov 24, 2011 at 14:55
  • @Marc B: A cursory look at the code he posted indicates that it's not working fine. His question can be summed up as "My code doesn't work. How can I changed it to make it work?" Commented Nov 24, 2011 at 14:56
  • @Asaph: yes, and perhaps the OP should actually say what the problem is. I can see that he's using $removeBreak without ever defining it, but without know what the OP actually wants this code to do, that could just be a bit of dead code. Commented Nov 24, 2011 at 14:57

3 Answers 3

1

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
)
Sign up to request clarification or add additional context in comments.

10 Comments

if($function=="query" && in_array($someID, $saleList)){ // }
I hate that type of output. I just want it the output I showed above.
I want to do my if statement above.
If you don't like that output, simply remove the line print_r($saleList); which is only there to prove that the code does what it's supposed to do. The $saleList variable already contains the array you're looking for.
The output is not important. The point is that your file has been parsed and your array has been created. Do you have what you need?
|
1

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.

Comments

0

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

3 Comments

This code works fine, however it prints ALL the PHP, it does not do the HTML.
@TheBlackBenzKid for the external file it will return HTML.
But how do I grab it into an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.