PHP 일기장
[PHP] 정규표현식 특정 문자 찾기
yong_zz
2020. 6. 15. 16:46
수학 문항에서 수식박스와 일반 글상자를 구분해서 순서를 매겨주는 정규표현식이다.
기록용으로 남겨둔다.
function search_answer_box( $html, &$math_box_index, &$box_index )//단답형 처리 add jy
{
preg_match_all( "/ccbox\[[0-9]+:[0-9]+]\{...\}/", $html, $matches, PREG_OFFSET_CAPTURE );
preg_match_all( "/cc-input/",$html, $cmatches, PREG_OFFSET_CAPTURE );
//match_all 은 다차원 배열 match => 1차원
if( count($matches[0]) >= ($math_box_index+1) && count($matches[0][$math_box_index]) >= 1) //수식박스
{
$attr = $matches[0][$math_box_index][0];// 이것은 위에서 추출된 ccbox[1:1]{①} 이런형태의 문자열
preg_match("/ccbox\[[0-9]+:[0-9]+]\{(.*?)\}/", $attr, $match);// ccbox[1:1]{?} ex) ? 에 해당하는 숫자 가져오기
if( count( $match ) >= 1 )
{
$temp = $match[1];
if( mb_strlen( $temp ) == 1 && preg_match("/[^a-z0-9]/i", $temp ) )
{
preg_match("/ccbox\[[0-9]+:(.*?)]\{.*\}/", $attr, $match);// ccbox[1:1]{?} ex) ? 에 해당하는 숫자 가져오기
if( count( $match ) >= 1 )
{
$temp = $match[1];
$math_box_index ++;
return "cc-minput-" . sprintf( "%02d", $temp );
}
}
}
}
else if( count( $cmatches[0] ) >= ($box_index+1) && count( $cmatches[0][$box_index] ) >= 1 )//polygon
{
$cut_html = substr( $html, 0, $cmatches[0][$box_index][1] );
preg_match_all( "/cc-input\"/", $cut_html, $match, PREG_OFFSET_CAPTURE);
$box_index ++;
if( count($match) >= 1 && count($match[0]) >= 1 )
return "cc-input-".sprintf( "%02d", count($match[0]) + 1);
else
return "cc-input-01" ;
}
return "";
}