Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Isolate a line and modify it [solved]
I am getting good at reading and writing to a text file using PHP but I have hit a problem I can't solve because all the examples I have found don't quite work and I can't see why.
Put as simply as I can, what I want to do is extract the keywords from this file, modify them then write them back to the file without modifying any of the other contents...
For reasons I won't go into, this needs to be done directly to the file, not via an extension.
Anyone with some spare time or a pointer to an example that will work please?
Put as simply as I can, what I want to do is extract the keywords from this file, modify them then write them back to the file without modifying any of the other contents...
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>My problem</title>
<link href="../css/main.css" rel="stylesheet" type="text/css" media="all">
<meta http-equiv="refresh" content="1;URL=index.php">
<meta name="keywords" content="test, try out, example, test ">
</head>
<body>....and the rest...
For reasons I won't go into, this needs to be done directly to the file, not via an extension.
Anyone with some spare time or a pointer to an example that will work please?
Posted: Tuesday, 8 May 2007 at 9:42AM
0
This discussion has been closed.
Comments
I am trying to get everything between <meta name="keywords" content="
and ">
...which I have achieved using the script below, but putting back the modified content is proving brain-draining...
// Extract Keywords $srchFile = 'srch.html'; $start = '<meta name="keywords" content="'; $end = ' ">'; $fp = fopen ($srchFile, 'r') or die('Unable to open file '.$srchFile.' for reading'); while (!feof ($fp)) { $buf = trim(fgets($fp, 4096)); $cont .= $buf; } preg_match("/$start(.*)$end/s",$cont,$match); echo 'Your Keywords are: '.$match[1];
Posted: Tuesday, 8 May 2007 at 11:22AM
What you suggested is what I have been trying to do, it's a little more complicated because I need to replace the <body> section too, but one step at a time for me!
Posted: Tuesday, 8 May 2007 at 3:00PM
Posted: Tuesday, 8 May 2007 at 5:39PM
The hassle I'm having is replacing the bits, putting them together and writing back out.
I'll persist in looking for an example.
Posted: Tuesday, 8 May 2007 at 8:07PM
Posted: Tuesday, 8 May 2007 at 9:23PM
Posted: Tuesday, 8 May 2007 at 9:35PM
$extension = file(trim($panelextensions[$SortOrder[$i]])); foreach ($extension as $line_num => $line) { if (strstr($line,"\$Panel->AddString(") || isset($looking)) { if (substr(trim($line),-1,1) == ";") { $parts = explode(',', $line); if ($parts[0] == $line) { $parts = explode(')',$line); } $line = $parts[0].",".$i.");\r\n"; unset($looking); } else { $looking = 1; } } else if (strstr($line,"\$Panel->AddList(") || isset($looking)) { if (substr(trim($line),-1,1) == ";") { $parts = explode(',', $line); if ($parts[0] == $line) { $parts = explode(')',$line); } $line = $parts[0].", \$Position = '".$i."', \$ForcePosition = '1');\r\n"; unset($looking); } else { $looking = 1; } } $extensioncontents .= $line; } $replace = fopen(trim($panelextensions[$SortOrder[$i]]),'w+'); fwrite($replace, $extensioncontents); fclose($replace);
is what I use in the panel order script to do the job. You'd want to concern yourself with something similar, e.g:
$file = file(file.txt); foreach ($file as $line_num => $line) { if (CHECK WHETHER THE LINE HAS THE DESIRED CONTENT) { $line = NEW CONTENTS OF THE LINE; } $filecontents .= $line; } $replace = fopen(file.txt,'w+'); fwrite($replace, $filecontents); fclose($replace);
That help atall?
Will let you know how I go.
Posted: Tuesday, 8 May 2007 at 10:07PM
I did solve the problem, I split the file into chunks, some static, some variable, all saved in separate text files.
Thanks again to all.
It's very satisfying when a plan comes together!
Posted: Wednesday, 9 May 2007 at 5:16PM