PHP: Getting part of string after and before given sub-string or character
Reviewed by Unknown
on
December 20, 2017
Rating:
function strafter($string, $substring) {
$pos = strpos($string, $substring);
if ($pos === false)
return $string;
else
return(substr($string, $pos+strlen($substring)));
}
function strbefore($string, $substring) {
$pos = strpos($string, $substring);
if ($pos === false)
return $string;
else
return(substr($string, 0, $pos));
}
$myvar = 'Anti,Christ,World';
$myvar = strafter($myvar,','); //$myvar = 'Christ,World'
echo $myvar; //result 'Christ,World'
echo strbefore($myvar,','); //result 'Christ'
<?php
function strafter($string, $substring) {
$pos = strpos($string, $substring);
if ($pos === false)
return $string;
else
return(substr($string, $pos+strlen($substring)));
}
function strbefore($string, $substring) {
$pos = strpos($string, $substring);
if ($pos === false)
return $string;
else
return(substr($string, 0, $pos));
}
$myvar = 'Anti,Christ,World';
$myvar = strafter($myvar,','); //$myvar = 'Christ,World'
echo $myvar; //result 'Christ,World'
echo strbefore($myvar,','); //result 'Christ'
?>
No comments: