0

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,

3
Contributors
7
Replies
10
Views
3 Years
Discussion Span
Last Post by Borzoi
0

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.

0

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

1

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.

0

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.

0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.