Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    lol
    lol is offline
    New to the CF scene
    Join Date
    Sep 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow trim () alternative function ?

    Hi!
    can someone help me make an alternative trim() function.
    the reason I need it, because trim($text, ","); works only on PHP v4.1.0 and higher.

    so, I was trying use preg_replace(), but since I s@x with REGEX, it didnt work properly.
    here is a working example:
    Code:
    function trim_alt($text, $what) {
      return preg_replace("/^($what)?(.*)($what)?$/ms", "\\2", $text);
    }
    $text = ".I want strip all '.' \n from this text.".
            "But it doesnt work.";
    echo "1- ".$text."<br>2- ".trim($text, ".")."<br>3- ".trim_alt($text, ".");
    printed number 2 and number 3 should be identical, but 3 doesnt strip dot from the end...what did I miss?
    or maybe there is a better way to do so?
    thank in advance.

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't understand well the question but if what you want is to strip the dots you can try:
    PHP Code:
    $text ".I want strip all '.' \n from this text.".
            
    "But it doesnt work.";

    echo 
    str_replace(".","",$text); 
    trim() is usually used to eliminate extra spaces.

  • #3
    lol
    lol is offline
    New to the CF scene
    Join Date
    Sep 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, trim() without second parameter will strip spaces at the both ends. And trim($string, "."); will strip dots from both ends, but in PHP older then 4.1.0 this second parameter (in my example its ".") is not implimented. Because of that, I'm looking for a "custom" function that would be alternative to trim(), that would do exactly what trim() does, but would work with any PHP version.
    In your example str_replace() will strip ALL dots from the sting, but all I need is strip dots from the both ends only.

    P.S. just realised, that in my first post example sais "strip all '.' from the string", but it should be "strip '.' from both ends of the string", sorry for the confusion, and thanks for the try

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php

    $text 
    ". bla . bla . bla .";

    $expression "/(^[\"|.]|[\"|.]$)/";

    $newtext preg_replace($expression''$text);

    echo 
    $newtext;

    ?>
    But the dots must be in the very first place of the expression ($text) or in the very last.

    found in php.net
    Last edited by maltrecho; 09-18-2003 at 04:53 AM.

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So, the function that u are looking for would looks like this:
    Code:
    function trim_alt($text, $what) {
      return preg_replace("/(^[$what]*|[$what]*$)/", "", $text);
    }


  •  

    Posting Permissions

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