예를 들어 문자열이 있습니다.
"오늘 영양이 배고프다";
"hungry"를 검색하고 앞에있는 3 개의 문자를 "xxx"로 바꾸고 싶습니다. 따라서이 경우의 최종 결과는 다음과 같습니다.
"오늘은 영양 xxxhungry"
substr_replace 함수가 작동하거나 strlen과 결합 할 수 있다고 생각했지만 얻지 못했습니다. 감사합니다.
당신은 사용할 수 strpos()
와 substr_replace()
함께 원하는 동작을 얻을 :
$str = "The antelope is hungry today"; // str to search
$needle = "hungry"; // str to search for
// returns beginning index of 'hungry' (16 in this case)
$start_index = strpos($str, $needle);
$result = substr_replace($str, "xxx", ($start_index - 3), 3);
echo $result; // prints : The antelope xxxhungry today
내 작업 예제는 이 phpfiddle 을 참조하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다