SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Function

  1. #1
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Function

    Hi, I have tried unsuccessfully to create a function that will strip the forward slashes ("/") and periods (".") from a string. Could someone please help me out with the correct way to do it?

    Thanks,
    -Jeff

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a simple function:

    PHP Code:
    <?php

        
    function seanstrip($string) {

            
    $string str_replace("."""$string);
            
    $string str_replace("/"""$string);

            return 
    $string;

        }


        
    $strip seanstrip("This is a full stop. and this is a forward slash /");

        echo 
    $strip;


    ?>
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Sean, thanks for your help

    -Jeff

  4. #4
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a shorter version:
    Code:
    <?php
        function paulstrip($string) {
          return preg_replace("/\.|\//", '', $string);
        }
    ?>
    You need to escape the dot character and the backslash(. = any character, \. = just a dot).
    Last edited by Paul S; Oct 6, 2001 at 14:42.

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am going to have to pull rank (as a mentor)

    1) No you don't need to escape the dot

    2) There will be no paulstrip functions around here, they will be seanstrip or nothing!

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by seanf
    I am going to have to pull rank (as a mentor)
    1) No you don't need to escape the dot
    mm, yes you have. Check the examples here


    2) There will be no paulstrip functions around here, they will be seanstrip or nothing!
    NOOO!!!!!

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Paul S
    mm, yes you have. Check the examples here
    Please try this:

    Code:
    <?php
    
        $string = "This wont be replaced >. but this will >\.";
    
        $string = str_replace("\.", "", $string);
    
        echo $string;
    
    ?>
    This is because str_replace is matching \. not .

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wops!!, I was thinking all the time in regular expressions

    [edit]Function above fixed

  9. #9
    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)
    i find it's faster (literally) to do it like Paul's code - looking through the string once is faster than looking through it twice w/ str_replace(). actually, if you want to get down to the details (or something...), this would be the best:

    PHP Code:
    $txt preg_replace('#[./]+#'''$txt); 
    it's better to use a character class than alternation w/ a pipe.
    - 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
  •