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.
Options

Isolate a line and modify it [solved]

edited May 2007 in Vanilla 1.0 Help
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...

<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

Comments

  • Options
    edited May 2007
    XML dom walking using php is way to go. u will be able to tell it eaxctly which tag to edit. without much hassle. Only hassle is u don't get the xml libraries with php 4. only php5. http://devzone.zend.com/node/view/id/1713#Heading8 it has examples on how to read and write to DOM and how to use html instead of xml.
  • Options
    Thanks mate but I'm still on PHP 4.
    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

  • Options
    easy way is to read the whole file split it in 3 parts Part 1: everything before meta tag Part 2: Meta tag Part 3: Everything after meta tag Change part 2 only and merge all the parts and then save the whole thing to file.
  • Options
    I don't suppose you have any example snippets?
    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

  • Options
    Have you checked out my panel re-order script? I know you dont want an extension etc but that does almost exactly what you're talking about just in a different context. I dunno if the code might seem a bit complex but it's probably about as simple as possible...if you want to take a look and get some ideas or dig bits out and question me on why i did something feel free...
  • Options
    Thanks mate, that's way too complex for me and scattered over 3 files, I'm pretty good at hacking but I know my limitations.

    Posted: Tuesday, 8 May 2007 at 5:39PM

  • Options
    edited May 2007
    Wanderer. ur regular expression will fail if the meta tag is in form <meta content="test, try out, example, test " name="keywords" > or this one <meta name='keywords' content='test, try out, example, test ' > that is going to be an even bigger problem once u get about fiddling with the body tag u have to find a library that will allow u to extract node elements from html and put it back. Searching Sourceforge. u will find lot of libraries that u can use on shared hosting http://sourceforge.net/projects/domit-xmlparser/ http://sourceforge.net/projects/phpxpath/ http://sourceforge.net/projects/phpdomxml/
  • Options
    No problem finding the lines and displaying them, I escape the double-quotes and the slashes.
    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

  • Options
    when you say you want to modify the tags does it matter what they were to begin with or do you want to change them all to a set thing?
  • Options
    Does not matter what they are, I need to read them into an editable field in a form to be modified (to whatever the user needs) and then written back without touching the other lines.

    Posted: Tuesday, 8 May 2007 at 9:23PM

  • Options
    In that case what I'd suggest you do is read all the lines of a file one by one checking them for any necessary content, edit them as appropriate, and then append them to a variable. Then just overwrite the entire file with the contents of the variable. That make sense?
  • Options
    Yes, that is exactly what I am trying to do but I need some sample code to look at, I am having trouble understanding the structure and sequence of the methods.

    Posted: Tuesday, 8 May 2007 at 9:35PM

  • Options
    $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?
  • Options
    Thanks mate, I'm working on a system where the changed lines are stored in an external txt file.
    Will let you know how I go.

    Posted: Tuesday, 8 May 2007 at 10:07PM

  • Options
    Sincere thanks to all who helped both publicly and via email, your suggestions were spot-on I'm sure but my php skills are a little under par to take advantage of your complex examples.

    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

This discussion has been closed.