Php 使用搜索替换多个文件获取内容参数

Php 使用搜索替换多个文件获取内容参数,php,replace,file-get-contents,Php,Replace,File Get Contents,和朋友一起做愚人节的笑话。基本上,我们是file_get_contents()本地报纸文章并执行str_replace()用于特定名称和图像URL。替换什么都不重要,我面临的问题是执行多个搜索/替换值 我写了两个函数,每个函数都有自己的变量和search/replace,只有第一个可以工作。我在琢磨如何获得多个搜索/替换值。下面是我正在使用的基本代码。如何添加多个搜索/替换 // Let's April Fool! $readfile = 'URL-police-searching-for-ba

和朋友一起做愚人节的笑话。基本上,我们是
file_get_contents()本地报纸文章并执行
str_replace()用于特定名称和图像URL。替换什么都不重要,我面临的问题是执行多个搜索/替换值

我写了两个函数,每个函数都有自己的变量和search/replace,只有第一个可以工作。我在琢磨如何获得多个搜索/替换值。下面是我正在使用的基本代码。如何添加多个搜索/替换

// Let's April Fool!
$readfile = 'URL-police-searching-for-bank-robbery-suspect';
$pull = file_get_contents($readfile);
$suspectname = 'Suspect Name';
$newname = 'New Name';
echo str_replace($suspectname, $newname, $pull);
我想搜索/替换页面上的内容,如名称、img src URL等。无论我要替换什么,如果我可以得到多个搜索/替换,我可以计算出详细信息


谢谢大家!

要进行考虑语法和自然语言的复杂替换,您需要更复杂的东西。但对于简单的搜索和替换,这将很好地工作:

<?php

$pull = "the suspect was last seen wearing a black hoodie and trainers. Have you seen this man? www.local_police.com/bank_robber.jpg";
echo $pull . "\n";

$dictionary = array("the suspect" => "friend_name", "hoodie" => "swimsuit", "trainers" => "adult diaper", "www.local_police.com/bank_robber.jpg" => "www.linkedlin.com/friend_profile_pic.jpg" );

foreach ($dictionary as $key => $value){
    $pull = str_replace($key, $value, $pull);
}

echo $pull . "\n";
?>

要进行复杂的替换,将语法和自然语言考虑在内,您需要更复杂的东西。但对于简单的搜索和替换,这将很好地工作:

<?php

$pull = "the suspect was last seen wearing a black hoodie and trainers. Have you seen this man? www.local_police.com/bank_robber.jpg";
echo $pull . "\n";

$dictionary = array("the suspect" => "friend_name", "hoodie" => "swimsuit", "trainers" => "adult diaper", "www.local_police.com/bank_robber.jpg" => "www.linkedlin.com/friend_profile_pic.jpg" );

foreach ($dictionary as $key => $value){
    $pull = str_replace($key, $value, $pull);
}

echo $pull . "\n";
?>

@jDo回答了我的问题,让我开始了,但我想分享最终的产品。以下是我的想法:

//
// Using file_get_contents(); in an array   
//
//
// Define the file we want to get contents from in a variable
//
$readfile = 'http://adomainname.com/page/etc';
//
// Variable to read the contents of our file into a string
//
$pull = file_get_contents($readfile);
//
//
// Define stuff to be replaced
//
$find_stuff = 'Find Stuff';
$replace_stuff = 'Replace Stuff';
$find_more_stuff = 'Find More Stuff';
$replace_more_stuff  = 'Replace More Stuff';
//
// Variable to create the array that stores multiple values in one single variable
//
$find_replace = [ // Let's start our array
                $find_stuff => $replace_stuff,
                $find_more_stuff => $replace_more_stuff,
                ];// Close the array

//
// This is where the magic happens
//
foreach ($find_replace as $key => $value){ // Now we take our array of each key and value
$pull = str_replace($key, $value, $pull); // Will look for matched keys and replace them with our new values 
}

echo $pull; // That's it

@jDo回答了我的问题,让我开始了,但我想分享最终的产品。以下是我的想法:

//
// Using file_get_contents(); in an array   
//
//
// Define the file we want to get contents from in a variable
//
$readfile = 'http://adomainname.com/page/etc';
//
// Variable to read the contents of our file into a string
//
$pull = file_get_contents($readfile);
//
//
// Define stuff to be replaced
//
$find_stuff = 'Find Stuff';
$replace_stuff = 'Replace Stuff';
$find_more_stuff = 'Find More Stuff';
$replace_more_stuff  = 'Replace More Stuff';
//
// Variable to create the array that stores multiple values in one single variable
//
$find_replace = [ // Let's start our array
                $find_stuff => $replace_stuff,
                $find_more_stuff => $replace_more_stuff,
                ];// Close the array

//
// This is where the magic happens
//
foreach ($find_replace as $key => $value){ // Now we take our array of each key and value
$pull = str_replace($key, $value, $pull); // Will look for matched keys and replace them with our new values 
}

echo $pull; // That's it

您可以通过将键(要替换的单词)映射为值(应该编写的内容)来创建一个类似于字典的数组:
$arr=array(“suspect”=>“friend\u name”)。然后循环遍历该数组,并为每次迭代重新定义
$pull
$pull=str\u replace($key,$value,$pull)。搜索“how to iterate a array in php”或类似内容,以获取示例和正确语法。@jDo是否适用于图像URL?这些不完全是单词。@jamerson当然,如果找到任何匹配的字符串,包括URL,都将被替换为数组中该键下的任何值,那么这将起作用。这是一个相当硬编码的解决方案,因为您必须将所有URL或URL子字符串放入数组中,但它会work@jDo杰出的我在办公室的第一件事就是让它发挥作用。我只需要更换三四件东西。谢谢。您可以通过将键(要替换的单词)映射为值(应该编写的内容)来创建一个类似于字典的数组:
$arr=array(“suspect”=>“friend_name”)。然后循环遍历该数组,并为每次迭代重新定义
$pull
$pull=str\u replace($key,$value,$pull)。搜索“how to iterate a array in php”或类似内容,以获取示例和正确语法。@jDo是否适用于图像URL?这些不完全是单词。@jamerson当然,如果找到任何匹配的字符串,包括URL,都将被替换为数组中该键下的任何值,那么这将起作用。这是一个相当硬编码的解决方案,因为您必须将所有URL或URL子字符串放入数组中,但它会work@jDo杰出的我在办公室的第一件事就是让它发挥作用。我只需要更换三四件东西。谢谢,上面写着“光辉”。我会测试并让你知道。再次感谢。你提供的东西为我指明了正确的方向。我将发布一个反映我提出的基本语法的答案。我不确定SE是否会在热链接到一篇完整的评论时遇到问题,但现在开始@贾默森·尼斯:)我一定要小心这个疯子!顺便说一句,谢谢你的信用。谢谢你的帮助。在我的代码中,
“\n”
是不必要的,并显示在页面底部。它显示为brilliance。我会测试并让你知道。再次感谢。你提供的东西为我指明了正确的方向。我将发布一个反映我提出的基本语法的答案。我不确定SE是否会在热链接到一篇完整的评论时遇到问题,但现在开始@贾默森·尼斯:)我一定要小心这个疯子!谢谢你的信用。谢谢你的帮助。在我的代码中,
“\n”
是不必要的,并显示在页面底部。