eregi question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Donny D

    eregi question.

    Hi!,
    I am trying to create a script which will the contents of a file and trim
    away the 'top' and 'bottom' of it.
    So far I have the following:--

    <?php
    $file = fopen("/var/www/html/file.txt", "r");
    $content = file_get_conten ts($page);

    fclose($file);

    ?>


    I would like to add two variables to this (like $start and $end) where
    anything before $start would be trimmed and anything after $end will be
    trimmed.

    Any idea how i can do this ?

    Sorry if this is a stupid question :)

    Thanks Don
  • Nick Derevjanik

    #2
    Re: eregi question.

    Donny D <dddd@ddddnospa m.com> wrote in
    news:c40u9b$46k $1@titan.btinte rnet.com:
    [color=blue]
    > Hi!,
    > I am trying to create a script which will the contents of a file and
    > trim away the 'top' and 'bottom' of it.[/color]


    Don,
    If PHP is resting on a Unix or Unix-like server you could use the "tail"
    and "head" functions provided by the OS. Say you wanted the middle 50 lines
    of a 100 line document you could do something like "head 75 | tail 50" and
    if my logic/math is correct that would give you the middle 50 lines...

    Hope that helps,
    - Nick

    Comment

    • oleg

      #3
      Re: eregi question.

      Hi,

      I think what you need is the following:


      $startpos = strpos($content , $start);
      $content = substr($content , $startpos);


      $endpos = strpos($content , $end);
      $content = substr($content , 0, $endpos);

      Just add this code before fclose();

      You will need to check for errors in case if $startpos or $endpos return
      FALSE.
      For more information you should read documentation on strpos() function.





      "Donny D" <dddd@ddddnospa m.com> wrote in message
      news:c40u9b$46k $1@titan.btinte rnet.com...[color=blue]
      > Hi!,
      > I am trying to create a script which will the contents of a file and trim
      > away the 'top' and 'bottom' of it.
      > So far I have the following:--
      >
      > <?php
      > $file = fopen("/var/www/html/file.txt", "r");
      > $content = file_get_conten ts($page);
      >
      > fclose($file);
      >
      > ?>
      >
      >
      > I would like to add two variables to this (like $start and $end) where
      > anything before $start would be trimmed and anything after $end will be
      > trimmed.
      >
      > Any idea how i can do this ?
      >
      > Sorry if this is a stupid question :)
      >
      > Thanks Don[/color]


      Comment

      • Donny D

        #4
        Re: eregi question.

        Thanks that works fine!

        oleg wrote:
        [color=blue]
        > Hi,
        >
        > I think what you need is the following:
        >
        >
        > $startpos = strpos($content , $start);
        > $content = substr($content , $startpos);
        >
        >
        > $endpos = strpos($content , $end);
        > $content = substr($content , 0, $endpos);
        >
        > Just add this code before fclose();
        >
        > You will need to check for errors in case if $startpos or $endpos return
        > FALSE.
        > For more information you should read documentation on strpos() function.
        >
        >
        >
        >
        >
        > "Donny D" <dddd@ddddnospa m.com> wrote in message
        > news:c40u9b$46k $1@titan.btinte rnet.com...[color=green]
        >> Hi!,
        >> I am trying to create a script which will the contents of a file and
        >> trim
        >> away the 'top' and 'bottom' of it.
        >> So far I have the following:--
        >>
        >> <?php
        >> $file = fopen("/var/www/html/file.txt", "r");
        >> $content = file_get_conten ts($page);
        >>
        >> fclose($file);
        >>
        >> ?>
        >>
        >>
        >> I would like to add two variables to this (like $start and $end) where
        >> anything before $start would be trimmed and anything after $end will be
        >> trimmed.
        >>
        >> Any idea how i can do this ?
        >>
        >> Sorry if this is a stupid question :)
        >>
        >> Thanks Don[/color][/color]

        Comment

        Working...