SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Austin
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing an .HTML file with PHP.

    Hello, I'm trying to wite a script that parses an .HTML page and formats its output to a file. I havent worked but 10 - 20 hours with PHP. This is the script:

    PHP Code:
    <?
            $matches
    [100];
            
    $string;
            
    $output "/home/tarts/public_html/test/file.txt";
            
    $file1;
            
    fopen($rh'r');
            if (!
    $file1 fopen($output'w+')) {
                    echo 
    "Cannot open file $output.\n";
                    exit;
            }
            if (!
    $string file_get_contents("https://rhn.redhat.com/errata/rh9-errata-security.html")) {
                    echo 
    "Unable to read file $file.\n";
                    exit;
            }
            if (!
    preg_match_all("200"$string$matchesPREG_SET_ORDER)) {
                    echo 
    "No matches, bad regex.\n";
                    exit;
            }
            if (!
    fwrite($file1$matches)) {
                    echo 
    "Unable to write to file $file1.\n";
                    exit;
            }
            if (!
    fclose($file1)) {
                    echo 
    "There has been an error, unable to close $file1.\n";
                    exit;
            }
            echo 
    "SUCCESS!\n";
            exit;
    ?>
    The error I'm getting is:
    Code:
    Warning: Delimiter must not be alphanumeric or backslash in /home/tarts/public_html/redhat.php on line 15
    No matches, bad regex.
    If anyone could help me with that error and also give a few general pointers on how to implement this I'd be happy to hear it.

    Thanks!

  2. #2
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try changing
    PHP Code:
    preg_match_all("200" 
    to
    PHP Code:
    preg_match_all("/200/" 
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  3. #3
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Austin
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by redux
    try changing
    PHP Code:
    preg_match_all("200" 
    to
    PHP Code:
    preg_match_all("/200/" 
    That rocks! One last thing, the output file contains the text...
    Code:
    Array
    It muct be a 'fwrite()' thing?

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because $matches is an array. Change $matches in the fwrite call to $string.
    And change preg_match_all to preg_match and drop the last two parameters, you don't use them anyway. Why do you use regexes anyway, a simple strpos would so the same way faster.

  5. #5
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Austin
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gaheris
    Because $matches is an array. Change $matches in the fwrite call to $string.
    And change preg_match_all to preg_match and drop the last two parameters, you don't use them anyway. Why do you use regexes anyway, a simple strpos would so the same way faster.
    Thanks, that works, last problem I have is the whole page is showing, not just lines of text starting with "200", any suggestion of a function?

    PS. The reason I wanted to use a regular expression is that is the way I hear you supposed to parse a file, but, I dont have experience with parsing files though.

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    PHP Code:
    <?php
    // Path
    $output '/home/tarts/public_html/test/file.txt';

    $fp fopen($output'w+') or die("Cannot open file '$output'.\n");
    $lines file('https://rhn.redhat.com/errata/rh9-errata-security.html') or die("Unable to read file.\n");
    foreach (
    $lines as $line) {
        if (
    substr($line03) == 200) {
            
    fwrite($fp$line) or die("Unable to write to file '$output'.\n!");
        }
    }
    fclose($fp) or die("There has been an error, unable to close '$output'.\n");

    echo 
    "SUCCESS!\n";
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •