Hi,

I have started using file_get_contents and regex, and after struggling have got it working. The issue I have is that when I print_r($match[1]); I get the results I want, but I do not know how to then convert this into something that I can store in a MySql database, as if it stores it just as 'Array'.

Here is my code with the specifics taken out, although I imagine this is just something straightforward.

$data = str_replace("\n", '', file_get_contents(''));
preg_match_all("",$data,$match);
$result = $match[1];

print_r($result);

Thanks,

Recommended Answers

Have you tried storing the results of print_r to a variable?

$newvariable = print_r($result);

I've not tried that myself but the theory here would be that the output is stored in the variable, which you can then pass to the database.

Jump to Post

This isn't a clean fix (someone may be able to provide a better one) but you could loop through the array, placing each entry of the array into an appended variable.

$newvariable = ""; //create the variable

for ($i= 0; isset($result[$i]); $i++)
{
    $newvariable .= $result.", "; …
Jump to Post

All 7 Replies

Have you tried storing the results of print_r to a variable?

$newvariable = print_r($result);

I've not tried that myself but the theory here would be that the output is stored in the variable, which you can then pass to the database.

Thanks for the response, I tried that and it inserted 'array' into the db again.

This isn't a clean fix (someone may be able to provide a better one) but you could loop through the array, placing each entry of the array into an appended variable.

$newvariable = ""; //create the variable

for ($i= 0; isset($result[$i]); $i++)
{
    $newvariable .= $result.", "; //Add the current array entry to the variable, appended with a comma and space.
}

The problem with this is that there will be a trailing comma and space at the end.

Hi pritaeas, the output is in the form:

Array ( [0] => One [1] => Two [2] => Three [3] )

Thanks Borzoi, I'll give that a go but I'm not entirely sure I understand it.

Hi,

I have found the solution, for future reference all you have to do is:

$newvariable = print_r($result,true);

and it is stored as that variable.

You're basically converting the array to a variable. Instead of the output being:

Array ( [0] => One [1] => Two [2] => Three [3] )

You're changing it so it becomes:

One, Two, Three, 

Like I said, it's not a clean fix and it's something I came up with in a second. I can describe what all parts of my code does in detail if you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.