SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replacing a variable

    Hi,
    I'm not sure if this the right place to post this question.. I'm new here..

    I'm looking for a php script or an advanced editor which replaces a variable with another txt or delete it
    let's say I have a text like this:

    68.24.15.15 526 ms
    98.14.12.54 542 ms
    94.21.61.25 541 ms

    what I need is a features replaces : *** ms
    which ***= can be any 3 numbers

    and the result will be only:
    68.24.15.15
    98.14.12.54
    94.21.61.25


    or if is it possible to import the IPs directly from the text file..

    any help would be appreciated..

    thanks!
    Last edited by Tr1x; Feb 10, 2005 at 14:28.

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You should be able to use a regular expression.

    PHP Code:
    $list file('textfile.txt');

    foreach( 
    $list as $item )
    {
      echo 
    preg_replace('/\s+\d{3}\s+ms$/i'''trim($item)), '<br />';

    Last edited by lilleman; Feb 8, 2005 at 09:12. Reason: Fixed the error in my script.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks lilleman,
    but getting an error in the script

    Code:
    Warning: Wrong parameter count for preg_replace() in /home/user/public_html/1.php on line 6

  4. #4
    SitePoint Enthusiast Aarakast's Avatar
    Join Date
    Feb 2005
    Location
    Germany
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
        $list 
    file('textfile.txt');

        foreach( 
    $list as $item )
        {
            echo 
    preg_replace('/\s+\d{3}\s+ms$/i','',trim($item)) . '<br />';
        }
    ?>
    He forgot to write, with which string, the time should be replaced...

    And if you don't want to use a regular expresion:
    PHP Code:
    <?php
        $list 
    file('textfile.txt');

        foreach( 
    $list as $item )
        {
            echo 
    substr(trim($item),0,-7) . '<br />';
        }
    ?>
    I think, the second script ist faster, but I didn't checked it.
    Greets,
    Pablo

    aarakast.de

  5. #5
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yup, it's faster (and therefore better) to use the string functions.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Faster and therefore better" is at least doubtful. What about reliability or flexibility or readability or any other "bility" you can imagine?

    With regexps he gets exactly what he wants (ie "IP address", not "seven chars from end") without any loops:

    PHP Code:
    $ip preg_replace(
        
    "~^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*~s"
        
    "$1"
        
    file('testfile.txt')
    );
    print_r($ip); 

  7. #7
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Well, I think it's better to use the string functions when possible.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you're wrong.

  9. #9
    SitePoint Enthusiast Aarakast's Avatar
    Join Date
    Feb 2005
    Location
    Germany
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I think stereofrog is right.

    But why should you use the slow regular expresion, if the list ist always the same??

    If the content, or the length of the list would change, regular expresions were better, but here it isn't like that.
    Greets,
    Pablo

    aarakast.de

  10. #10
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much guys, your help are really appreciated..

    the code worked after modifiction..

    stereofrog:
    the code you have wrote imports or gets ips from a txt file , right?

    I tested it , it worked but it shows some text with ips:
    Array ( [0] => 654.254.957.1 [1] => 654.254.957.2 [2] => 654.254.957.3 [3] =>654.254.957.4 [4]

    I'm sure I will need it in some text when the file has different text than ips..
    it would be better if it list ips..ip by line with no extra text like lilleman & Arakast code..

    thanks again..

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Short answer: replace print_r($ip); into print implode("<br>", $ip);

    Long answer:

    The "extra text" is a debugging information produced by print_r function. print_r is widely used in examples and testcases, because it shows what you're really "have", not what you "see". Just in case you bother to read the docs here the link: print_r

  12. #12
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool, worked fine now !

    thanks stereofrog

    php is really interesting and maybe I'll start learning it soon..


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
  •