Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中一次替换多个不同单词的更好方法_Php_Str Replace - Fatal编程技术网

在PHP中一次替换多个不同单词的更好方法

在PHP中一次替换多个不同单词的更好方法,php,str-replace,Php,Str Replace,我已经做了一段时间的文件获取内容代码,我真的厌倦了用我的方法一次替换多个不同的单词。有时,我的文件内容来源的网站改变了布局,因此不得不在混乱中改变 这是我正在使用的方法: $results = file_get_contents('https://example.com/'); $filter1 = str_replace('https://example.com', 'index.php', $results); $filter2 = str_replace('<scri

我已经做了一段时间的文件获取内容代码,我真的厌倦了用我的方法一次替换多个不同的单词。有时,我的文件内容来源的网站改变了布局,因此不得不在混乱中改变

这是我正在使用的方法:

$results = file_get_contents('https://example.com/');

$filter1 = str_replace('https://example.com', 'index.php',        $results);
$filter2 = str_replace('<script', '<!-- OVERWRITTEN ',            $filter1);
$filter3 = str_replace('</script>', ' OVERWRITTEN -->',           $filter2);
$filter4 = str_replace('src="http://example.com', 'src="',        $filter3);
$output = str_replace('<p>Some Text</p>', '<p>Something Else</p>',$filter4);

echo $output;
$results=file\u get\u contents('https://example.com/');
$filter1=str\u replace('https://example.com“,”index.php“,$results);
$filter2=str_replace(“”,$filter2);
$filter4=str\u replace('src='http://example.com“,”src=“”,$filter3);
$output=str_replace(“某些文本”

”、“其他内容”

”、$filter4); echo$输出;

一次替换多个不同的单词是否比我所做的更好、更干净?我不确定PHP处理这种混乱的额外延迟是什么

是的,您可以通过发送数组来实现:

$results = file_get_contents('https://example.com/');

$output = str_replace(
  array('https://example.com', '<script', '</script>', 'src="http://example.com', '<p>Some Text</p>'),
  array('index.php', '<!-- OVERWRITTEN ', ' OVERWRITTEN -->', 'src="', '<p>Something Else</p>'),
  $results
);

echo $output;
$results=file\u get\u contents('https://example.com/');
$output=str\u replace(

数组('https://example.com“,”也许只是使用一个数组?循环并用str_替换数组?@gabs我真的想避免任何循环,但数组可以是某种东西。@Typewar是的,数组FTW。我的解决方案有效吗?
$output = str_replace(
  array('https://example.com', '<script',           '</script>',        'src="http://example.com', '<p>Some Text</p>'),
  array('index.php',           '<!-- OVERWRITTEN ', ' OVERWRITTEN -->', 'src="',                   '<p>Something Else</p>'),
  $results
);