PHP fopen - Write a variable to a txt file

Viewed 816

I've checked this and its not working for me ! PHP Write a variable to a txt file

So this is my code,please take a look ! I want to write all the content of the variable to the file. But when i run the code,its only write the last line of the content !

<?php
$re = '/<li><a href="(.*?)"/';
$str = '
<li><a href="http://www.example.org/1.html"</a></li>
                        <li><a href="http://www.example.org/2.html"</a></li>
                        <li><a href="http://www.example.org/3.html"</a></li> ';

preg_match_all($re, $str, $matches);
echo '<div id="pin" style="float:center"><textarea class="text" cols="110" rows="50">';
// Print the entire match result

foreach($matches[1] as $content)
  echo $content."\r\n";
$file = fopen("1.txt","w+");
echo fwrite($file,$content);
fclose($file);
?>

When i open 1.txt,its only show me

http://www.example.org/3.html

It's should be

http://www.example.org/1.html
http://www.example.org/2.html
http://www.example.org/3.html

Am i doing wrong anything?

2 Answers
Related