SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace a word with a num

    I have a text like this:

    test test <num> test test <num> test test <num> ...

    I want to replace the <num> with a nummber say 0 and the next <num> will be replaced with 0 + 1 and the next 0 + 2, so I get somr thing like this:

    test test 0 test test 1 test test 2 ...

    Thanx
    - m0h

  2. #2
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's one way:
    PHP Code:
    <?php
    $oldString 
    'test test <num> test test <num> test test <num> ...';
    $newString '';
    $stringBits explode ('<num>'$oldString);

    while ( list(
    $key$value) = each($stringBits)) {
      
    $newString .= $value . (($key count($stringBits)-1) ? ($key) : (''));
    }

    echo 
    $newString// 'test test 0 test test 1 test test 2 ...'
    ?>
    although I'm sure there must be a better one.
    Last edited by FireFly; Feb 12, 2002 at 07:20.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whether its easier or not, I have no idea, but it should be faster.

    PHP Code:
    $i 0;
    $str 'test test <num> test test <num> test test <num> test test <num> test test <num>';
    $str preg_replace("/(<num>)/e"'str_replace("$1", $i++, "$1")'$str); 
    there should not be a space between <num> and ) but vb is sticking it in anyway.
    Last edited by freddydoesphp; Feb 12, 2002 at 11:56.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And much more elegant.

  5. #5
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx!
    - m0h

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    there should not be a space between <num> and ) but vb is sticking it in anyway.
    <offtopic>that's b/c vB turns > (and the like) into &amp;gt;, as you probably know. it then puts a space between ; in &amp;gt; and the following ) so that's it's not interpreted as . it should be handled better, rather than altering posts.</offtopic>

    sweet code, BTW.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


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
  •