
mb_ereg_search_getpos 함수는 PHP 7.2.0 버전부터 deprecated 상태입니다. 따라서 사용하지 않는 것이 좋습니다. 대신, preg_match_all 함수를 사용하여 정규표현식에 매치되는 문자열의 위치를 찾을 수 있습니다.
예를 들어, 'hello world' 문자열에서 'hello'가 시작되는 위치를 찾으려면 다음과 같이 할 수 있습니다.
#hostingforum.kr
php
$string = 'hello world';
$pattern = '/hello/';
preg_match_all($pattern, $string, $matches);
echo $matches;
위 예제에서는 preg_match_all 함수를 사용하여 'hello'가 시작되는 위치를 찾았습니다. $matches는 배열로 반환되며, 매치된 문자열의 시작 위치가 포함되어 있습니다.
또한, preg_match_all 함수는 0을 반환합니다. 이는 매치된 문자열이 없다는 것을 의미합니다.
#hostingforum.kr
php
$string = 'world';
$pattern = '/hello/';
preg_match_all($pattern, $string, $matches);
echo $matches === false ? '매치된 문자열이 없습니다.' : '매치된 문자열이 있습니다.';
위 예제에서는 preg_match_all 함수를 사용하여 'hello'가 시작되는 위치를 찾았습니다. 그러나 'hello'가 문자열에 없기 때문에 false가 반환됩니다. 따라서 매치된 문자열이 없다는 메시지를 출력합니다.
2025-04-07 10:17