PHP 함수 substr after, before, from, until

1 개요[ | ]

PHP 함수 substr after, before, from, until
function substr_after($needle, $haystack) { return substr( strchr($haystack,$needle), strlen($needle) ); }
function substr_before($needle, $haystack) { return strstr($haystack, $needle, true); }
function substr_from($needle, $haystack) { return strstr($haystack, $needle); }
function substr_until($needle, $haystack) { return ($pos=strpos($haystack,$needle))? substr($haystack,0,$pos).$needle : false; }

2 substr_after[ | ]

function substr_after($needle, $haystack) { return substr( strchr($haystack,$needle), strlen($needle) ); }
 
echo substr_after('ll', 'hello world');
echo substr_after('l', 'hello world');
echo substr_after('x', 'hello world'); // bool(false)
# o world
# lo world
#

3 substr_before[ | ]

function substr_before($needle, $haystack) { return strstr($haystack, $needle, true); }
 
echo substr_before('ll', 'hello world');
echo substr_before('l', 'hello world');
echo substr_before('x', 'hello world'); // bool(false)
# he
# he
#

4 substr_from[ | ]

function substr_from($needle, $haystack) { return strstr($haystack, $needle); }
 
echo substr_from('ll', 'hello world');
echo substr_from('l', 'hello world');
echo substr_from('x', 'hello world'); // bool(false)
# llo world
# llo world
#

5 substr_until[ | ]

function substr_until($needle, $haystack) { return ($pos=strpos($haystack,$needle))? substr($haystack,0,$pos).$needle : false; }

echo substr_until('ll', 'hello world');
echo substr_until('l', 'hello world');
echo substr_until('x', 'hello world'); // bool(false)
# hell
# hel
#

6 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}