www.webdeveloper.com
Results 1 to 8 of 8

Thread: How do you read a file up to a certain point?

  1. #1
    Join Date
    May 2003
    Posts
    29

    Red face How do you read a file up to a certain point?

    Let's say I hade a variable ($find_this_string="[next]") and I wanted to read everything in a file up to $find_this_string into a variable. How would I do this? I know you can read x number of charectors but what if it's a variable number of charectors? Also how do I get the length of a string(in charecters).

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    This will read through a .txt file and put all the content before [next] into the varialbe $good:

    PHP Code:
    <?PHP

    $filename 
    "test.txt";
    $find_this_string "\\[next\\]"# backslashes added due to the fact that [ and ] are regexp special characters.
    $new "";

    $contents = @file($filename) or die ("File $filename could not be opened.");

    foreach (
    $contents as $line_num => $line) {
        
    $new .= $line;
    }

    list (
    $good$bad) = preg_split("/$find_this_string/"$new);

    echo 
    $good;

    ?>
    and, to get the length of a string, you use strlen($string);

  3. #3
    Join Date
    May 2003
    Posts
    29
    it there a way to do that but leave the file pointer at the point the last phrase left off?

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Huh? I don't think I understand what you are trying to do. Could you please explain what you need to do a bit better?

  5. #5
    Join Date
    May 2003
    Posts
    29
    I am trying to read multiple paragraphs in to an array so I can edit them seperatly.(news stories)

  6. #6
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    So, you are opening a HTML file, and trying to pull certain <p>'s out so you can edit them? If this is the case, I would recommend adding comments to your page, like this <!--editable--> if possible. Then, the PHP file can split it at each <!--editable--> and you will have an array of each of the <p>'s you want to edit... Either way, what you are going to have to do when you write it back is something like this: (using my previous code, assumbing you want to replace the [next] with your text)

    PHP Code:
    <?PHP

    $filename 
    "test.txt";
    $find_this_string "\\[next\\]"# backslashes added due to the fact that [ and ] are regexp special characters.
    $text "This is the text to add";
    $new "";

    $contents = @file($filename) or die ("File $filename could not be opened.");

    foreach (
    $contents as $line_num => $line) {
        
    $new .= $line;
    }

    list (
    $good$bad) = preg_split("/$find_this_string/"$new);

    $write $good.$text.$bad;

    $fp fopen ($filename"w");
    if (
    $fp) {
        
    fwrite ($fp$text);
        
    fclose ($fp);
        echo (
    "File written");
    }
    else {
        echo (
    "File was not written");
    }

    ?>

  7. #7
    Join Date
    May 2003
    Posts
    29
    Actually I wan to read all the seperate <p>'s into an array so I can edit multiple ones and re arange the sequence they are in.

  8. #8
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    This will go through the file and pull all the <p>'s (note: must end with a </p>) from your file. It will then put them into the array $p.

    PHP Code:
    <?PHP

    $filename 
    "test.txt";
    $new "";

    $contents = @file($filename) or die ("File $filename could not be opened.");

    foreach (
    $contents as $line_num => $line) {
        
    $new .= $line;
    }

    preg_match_all("/\<p\>(.+?)\<\/p\>/"$new$p);
    #If you don't want to pull empty <p>'s (<p></p>) use the below regexp
    #preg_match_all("/\<p\>(.+?)\<\/p\>/", $new, $p);
    $p $p[0]; #set $p to be an array of all the <p>'s

    foreach ($p as $line_num => $line) {
        echo 
    $line#echo <p>'s to the screen
    }

    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles