- 09-07-2011 12:29 PM #1Junior 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,
I've used inbuilt array_shift and explode methods to achieve the desired resultPHP Code:<?php
function strstrb($haystack,$needle){
return array_shift(explode($needle,$haystack,2));
}
//example
echo strstrb('asdf,hkl,erty',',');
// output: asdf
?>
- 09-07-2011 12:36 PM #2Junior 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

It also gives same outputPHP 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;
}
?>
-
- 12-05-2011 04:16 AM #3WTF Groupie Array
- Join Date
- Oct 2011
- Posts
- 349
- Thanks
- 0
- Thanked 2 Times in 2 Posts






LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks