Results 1 to 3 of 3
  1. #1
    Junior Member Array
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP : A way to print text before a delimeter !

    I've found a way inphp through which you can print the text before a delimeter.
    For example, you have the string asdf,hkl,erty
    And you want to print anything before first comma, then you can use following php code,

    PHP Code:
    <?php
    function strstrb($haystack,$needle){
        return 
    array_shift(explode($needle,$haystack,2));
    }
    //example
    echo strstrb('asdf,hkl,erty',',');
    // output: asdf
    ?>
    I've used inbuilt array_shift and explode methods to achieve the desired result

  2. #2
    Junior Member Array
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts


    Another way which I would like to share.. It uses string reverse funtion

    PHP Code:
    <?php 
    //$needle is delimeter and $haystack is actual string 
    function strstrbef($haystack$needle

        
    $needle=strrev($needle); 
        
    $haystack=strrev($haystack); 
        
    $result=strstr($haystack,$needle); 
        
    $result=substr($haystack,strlen($needle)); 
        
    $result=strrev($result); 
        return 
    $result

    ?>
    It also gives same output

  3. #3
    WTF Groupie Array
    Join Date
    Oct 2011
    Posts
    349
    Thanks
    0
    Thanked 2 Times in 2 Posts


    thanks for the info!


 

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
  •