Link to home
Start Free TrialLog in
Avatar of James Williams
James WilliamsFlag for United States of America

asked on

PHP Write to file with variable in that file as the resulting NEW file name.


I have a complete coded php page with. This also on it.

Except the metadata formatting The keyword are not the same on any pages.

Page 1 example

<meta name="keyword" content=SMITH, JOHN, DOE, PLAIN, JAIN, MAIN, JOE, SOMEONE, WILLIAMS">

Page 2 example

<meta name="keyword" content=Mike Bill, Paul, Jenn, Ted">


What  I am attempting to do is  have PHP process the page and write it to a NEW TEXT file.
NAMING THE NEW FILE using the First 3 Meta Keywords in that file..

Basically Run a page thru PHP and having the result be a txt file  created and named using that files meta KEywords.
The two examples above would include the ordinal page code and be names as so.
smith-john-doe.txt  
mike-bill-paul.txt

The commas and space parsed into hyphens and no space.

I have attempted to solve this myself with some good results.
But not what I needed.


$myFile = "KEYWORDS.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "HELP";
fwrite($fh, $stringData);
fclose($fh);





The pages if needed can have the new code I anticipate as the ONLY PHP script on that page.




The page is processed via PHP using this code.

Thank you agian Ray_Paseur

https://www.experts-exchange.com/codeSnippetPopup.jsp?csid=348721

REGARDS AND thank you.

Selvol
Avatar of DerkArts
DerkArts
Flag of Netherlands image

I'm not sure about the rest of your post and what your saying. But what you need is this.

I havent tested this but it should work



//Loop over files, in whatever way you like
foreach($fileArray as $fileName){
	$str = file_get_contents($fileName);
	preg_match('%<meta\sname="keyword"\scontent="([^"]*)"%/i',$str,$matchArray);
	$namesArray = explode(",",$matchArray[1]);
	$file = fopen($namesArray[0].$namesArray[1].$namesArray[3].".txt","w+");
	fwrite($file,$str);
	fclose($file);
	
}

Open in new window

Avatar of James Williams

ASKER

Apologies for any confusion with the question. My fault The script was not working.
THank you thought
All I am trying to do is.
Have PHP process a file and make a copy with a name generated by the Keywords of the page it just processed.
____
Folder dir
a.php  
Folder after file processed with php
Folder dir
a.php   manuel-madsen-michael.php
manuel-madsen-michael.php is spanwed form a.php
But PHP used "a.php" Keywords to name the spawned file.    
 

a.php
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="keywords" content=" MANUEL,MADSEN, MICHAEL,HELMAN, AYERS, MILTON,NICOSIA, NANCY LYNN" />
...
 
...</html>
 
manuel-madsen-michael.php
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="keywords" content=" MANUEL,MADSEN, MICHAEL,HELMAN, AYERS, MILTON,NICOSIA, NANCY LYNN" />
...
 
...</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DerkArts
DerkArts
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, I never thought your answer was wrong.  And I thank you again.
I did get the script to work.
I had to change the preg_match line to work with my inconstancy plagued HTML.
Only thing is, and I most likely need to figure it out is how to get the saved file into LOwercase and add hyphens
between the arrays in the name.
NO NEED to tell me how to do the LOWER CASE and hyphens.
As I actually want to learn PHP and not have some extremely considerate person such as yourself do everything for me.
I really appreciate the time you spent on the question. And I thank you.
 
Regards
Selvol

<?php
$str = file_get_contents("a.php");
        preg_match('%<meta name=\"keywords\" content=\"([^"]*)"%i',$str,$matchArray);
        $namesArray = explode(",",$matchArray[1]);
        $file = fopen($namesArray[0].$namesArray[1].$namesArray[3].".txt","w+");
        fwrite($file,$str);
        fclose($file);
?> 

Open in new window