이것에 대한 정규식을 피하기 위해 더 좋을 것입니다 작업 그냥 사용 str_replace()
:
입력 :
$str = 'button_reset,location_text,email_text';
코드 배열로 출력 :
var_export(explode(',',str_replace(['button_reset','location_text','email_text'],['reset','text','email'],$str)));
// array (
// 0 => 'reset',
// 1 => 'text',
// 2 => 'email',
//)
또는 당신이 주장하는 경우, 정규식 (Demo Link) :
/button_\K[^,]+|,location_\K[^,]+|,\K[^_]+(?=_text)/
정규식 고장 :
button_\K[^,]+ #Match one or more non-comma-characters after button_
| #or
,location_\K[^,]+ #Match one or more non-comma-characters after location_
| #or
,\K[^_]+(?=_text) #Match one or more non-underscore-characters that are
# immediately followed by _textafter button_
각 조건식의 \K
이 점에서 일치를 의미 효과적으로이 경우에 캡처 그룹을 사용할 필요가 제거합니다. 캡처 그룹을 사용하는 경우 preg_match_all()
은 전체 문자열 일치로 채워지는 하위 배열과 캡처 된 값으로 적어도 하나 이상의 하위 배열을 만듭니다. \K
은 배열 크기를 최대 50 %까지 줄일 수 있으므로 가능한 한 항상 사용해야합니다.
코드 :
$array=preg_match_all('/button_\K[^,]+|,location_\K[^,]+|,\K[^_]+(?=_text)/',$str,$out)?$out[0]:[];
var_export($array);
같은 출력 :
는
array (0 => 'reset', 1 => 'text', 2 => 'email',)
귀하의 예상 출력은 이해가되지 않습니다. "전자 메일"은 밑줄 뒤에 표시되지 않습니다. –