Jump to content

Recommended Posts

Hi,

 

I have an array with the following structure

 

1 => Title: etc etc, Authors: etc etc, Filename: etc etc

2 => Title: etc etc, Authors: etc etc, Filename: etc etc

3 => Title: etc etc, Authors: etc etc, Filename: etc etc

4 => Title: etc etc, Authors: etc etc, Filename: etc etc

5 => Title: etc etc, Authors: etc etc, Filename: etc etc

 

I have been trying (without success) to save each value as a text file with the title and authors as the content and the filename as the filename. I can easily isolate the needed texts with regex (so don't waste your time on that part) but am struggling with the "foreach" PHP part. Please help (!)

 

Thanks for reading,

 

Will

Link to comment
https://forums.phpfreaks.com/topic/70954-save-array-values/
Share on other sites

Not one file. one file for each title.

 

here is how i might do it:

 

foreach ($somearray AS $someinfo) {

   $title_str = howeveryouregettingthetitle();

   $author_str = howeveryouregettingtheauthor();

   $filename = howeveryouregettingthefilename();

 

   $fh = fopen("/path/to/$filename", "w") or die("fopen failed");

   fwrite($fh, "$title_str $author_str");

   fclose($fh);

}

Link to comment
https://forums.phpfreaks.com/topic/70954-save-array-values/#findComment-356716
Share on other sites

Hi,

 

The code that I tried (putting in your code as well) is below. I'm still going wrong somewhere unfortunately. The below code produced one text file (instead of many) with the correct content for that particular value but no filename (it was just ".txt"). Any ideas?

 

foreach ($replace10 as $key => $val)
{
$matchtitle = preg_match("@TITLE\:\s(.*?)\r\n@", $val, $outtitle);
$title = preg_replace("@TITLE\:\s@", "", $outtitle[0]);
$matchauthors = preg_match("@AUTHORS\:\s(.*?)\r\n@", $val, $outauthors);
$authors = preg_replace("@AUTHORS\:\s@", "", $outauthors[0]);
$matchcitation = preg_match("@CITATION\:\s(.*?)\r\n@", $val, $outcitation);
$citation = preg_replace("@CITATION\:\s@", "", $outcitation[0]);
$matchfile = preg_match_all("@FILE\:\s(.*?)\r\n@", $val, $outFILE);
$file = preg_replace("@FILE\:\s@", "", $outFILE[0]);

$desccontent = '<title>'.$title.' ('.$citation.') - '.$publisher.'</title>
<meta name="description" content="'.$authors.'">';

$fh = fopen("$file.txt", "w") or die("fopen failed");
    fwrite($fh, "$desccontent");
    fclose($fh);
}

Link to comment
https://forums.phpfreaks.com/topic/70954-save-array-values/#findComment-356735
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.