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

    Printing string before delimters using basic methods in PHP !

    I'll be using strpos to get the position and strtolower, so that the string isn't anymore case sensitive in my method . So, here you go,
    PHP Code:
    <?php
    function strstrbi($haystack$needle$before_needle=FALSE$include_needle=TRUE$case_sensitive=FALSE) {
     
    //Find the position of $needle
     
    if($case_sensitive) {
      
    $pos=strpos($haystack,$needle);
     } else {
      
    $pos=strpos(strtolower($haystack),strtolower($needle));
     }
     
     
    //If $needle not found, abort
     
    if($pos===FALSE) return FALSE;
     
     
    //Adjust $pos to include/exclude the needle
     
    if($before_needle==$include_needle$pos+=strlen($needle);
     
     
    //get everything from 0 to $pos?
     
    if($before_needle) return substr($haystack,0,$pos);
     
     
    //otherwise, go from $pos to end
     
    return substr($haystack,$pos);
    }
    ?>
    Now, just call strstrbi with required number of arguments and you get the output string which is before the first occurence of delimeter.

  2. #2
    Old School DNLodge Member Array
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts


    thanks for the information, i have been looking for this kind of stuff. i am facing a problem right now which is quite similar to this one.

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


    nice work!


 

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
  •