"함수 substrBefore()"의 두 판 사이의 차이

잔글 (Jmnote 사용자가 Rm from 문서를 함수 substr before 문서로 옮겼습니다)
1번째 줄: 1번째 줄:
;rm_from
[[category:String]]
[[category:String]]
;substr_before


==PHP==
==PHP==
[[category:PHP]]
[[category:PHP]]
<source lang='php'>
<source lang='php'>
function rm_from($needle, $haystack, $preservation = true) {
echo strstr('hello world', 'll', true);
$pos = strpos($haystack, $needle);
echo strstr('hello world', 'l', true);
if($pos !== false) return substr($haystack, 0, $pos);
echo strstr('hello world', 'x', true); // bool(false)
if($preservation) return $haystack;
# he
return false;
# he
}
#
$result1 = rm_from('ll', 'hello');
$result2 = rm_from('al', 'hello');
$result3 = rm_from('al', 'hello', false);
echo "[$result1]"; // [he]
echo "[$result2]"; // [hello]
echo "[$result3]"; // []
</source>
</source>
<source lang='php'>
<source lang='php'>
function rm_from($needle, $haystack, $preservation = true) {
function substr_before($needle, $haystack) { return strstr($haystack, $needle, true); }
$arr = explode($needle, $haystack, 2);
 
if( count($arr)<2 && !$preservation ) return false;
echo substr_before('ll', 'hello world');
return $arr[0];
echo substr_before('l', 'hello world');
}
echo substr_before('x', 'hello world'); // bool(false)
# he
# he
#
</source>
</source>


==같이 보기==
==같이 보기==
*[[rm_after]]
*[[함수 substr_until]]
*[[rm_before]]
*[[함수 substr_from]]
*[[rm_until]]
*[[함수 substr_after]]

2015년 2월 20일 (금) 17:16 판

substr_before

1 PHP

echo strstr('hello world', 'll', true);
echo strstr('hello world', 'l', true);
echo strstr('hello world', 'x', true); // bool(false)
# he
# he
#
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
#

2 같이 보기

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