파일이 있는데 한 단어를 다른 배열로 바꾸고 싶습니다. - 또는 텍스트 파일에 "고양이"와 "개"의 모든 인스턴스배열에서 단어 바꾸기. PHP
$arr = array(array("milk", "butter"), array("dog", "cat"))
그래서 내가 "버터"와 "우유"의 모든 인스턴스를 대체 할 예를 들어 나는 file.txt
과 배열을 가지고있다.
어떻게하면됩니까?
파일이 있는데 한 단어를 다른 배열로 바꾸고 싶습니다. - 또는 텍스트 파일에 "고양이"와 "개"의 모든 인스턴스배열에서 단어 바꾸기. PHP
$arr = array(array("milk", "butter"), array("dog", "cat"))
그래서 내가 "버터"와 "우유"의 모든 인스턴스를 대체 할 예를 들어 나는 file.txt
과 배열을 가지고있다.
어떻게하면됩니까?
이 코드는 각 내부 배열의 첫 번째 단어의 모든 항목을 두 번째 단어 (해당 항목)로 바꿉니다.
$txt = file_get_contents('file.txt'); //text example 'My dog loves milk. My cat loves butter.';
$words = array(array('milk', 'butter'), array('dog', 'cat'));
$result = $txt;
foreach($words as $word){
$result = str_replace($word[0], $word[1], $result);
}
echo 'Before: ' . $txt;
echo '<br>';
echo 'After: ' . $result;
file_put_contents('file2.txt', $result); // won't replace the file so you can see the difference.
출력 :
전에: 내 강아지가 우유를 좋아합니다. 내 고양이는 버터를 좋아해.
다음 : 내 고양이는 버터를 좋아합니다. 내 고양이는 버터를 좋아해.
주 :
* 예쁜 그루비 * –
다음과 같이 시도해 볼 수 있습니다.
<?php
// get file content
$text = file_get_contents("file.txt");
$arr = array(array("milk", "butter"), array("dog", "cat"));
foreach($arr as $val){
//replace text with your pattern
$text = str_replace($val[0],$val[1],$text);
}
echo $text;
파일 내용을 가져 와서 내용을 바꾸고 다시 저장 하시겠습니까?! – Rizier123