Php 用文件中的新url替换路径和引号
我有一个包含以下数据的文本文件:Php 用文件中的新url替换路径和引号,php,string,file,Php,String,File,我有一个包含以下数据的文本文件: "K:\data\etrdtCfhbr6MUkAAFuVw.jpg" "K:\data\rgtdrCfhbr6OUYAE5lNR.jpg" "K:\data\Cfhbr6VVIrdtdAAmRPr.jpg" "K:\data\Cffh-EyWsersetsAQ8eIz.jpg" 我想替换引号和部分路径以获得如下输出: http://myweb.com/dat/etrdtCfhbr6MUkAAFuVw.jpg http://myweb.com/dat/rgtdr
"K:\data\etrdtCfhbr6MUkAAFuVw.jpg"
"K:\data\rgtdrCfhbr6OUYAE5lNR.jpg"
"K:\data\Cfhbr6VVIrdtdAAmRPr.jpg"
"K:\data\Cffh-EyWsersetsAQ8eIz.jpg"
我想替换引号和部分路径以获得如下输出:
http://myweb.com/dat/etrdtCfhbr6MUkAAFuVw.jpg
http://myweb.com/dat/rgtdrCfhbr6OUYAE5lNR.jpg
http://myweb.com/dat/Cfhbr6VVIrdtdAAmRPr.jpg
http://myweb.com/dat/Cffh-EyWsersetsAQ8eIz.jpg
现在我有一些(伪)代码,我不知道如何让它正常工作:
$filecontents = file_get_contents('/path/to/file.txt'); //Load the file contents
$newcontent = preg_replace('.....', $filecontents); //Use a regex to replace the stuff as I want
file_put_contents('/path/to/file.txt', $newcontent);
$filecontents=file_get_contents('/path/to/file.txt')//加载文件内容
$newcontent=preg_replace(“……”,$filecontents)//使用正则表达式替换我想要的东西
文件内容('/path/to/file.txt',$newcontent);
我突出显示了当前卡在其中的代码。您可以将文件作为字符串使用。然后,您可以使用替换每个路径。然后,您只需将文件保存回原处 regex
/\“*\\\(.*?\”/m
仅表示:
匹配双引号(\”*\\\\
),然后匹配所有内容(\”
),直到最后一个反斜杠(*
)\\\
匹配所有内容((.*?\”
),直到出现双引号((.*?
)\”
修饰符m只是表示对每行使用正则表达式m
你被困在哪里?[2]@Rizier123你尝试过使用什么代码?我打赌手册$filecontents=file_get_contents('/path/to/file.txt')中有一个函数可以实现这一点//加载文件内容$newcontent=preg_replace(“……”,$filecontents)//使用正则表达式更改文件内容。替换(.${4})添加。并将文件扩展名返回到行中。文件内容('/path/to/file.txt',$newcontent);我不知道用过preg_替换吗
<?php
$file = file_get_contents("test.txt");
$file = preg_replace("/\".*\\\\(.*?)\"/m", "http://myweb.com/dat/$1", $file);
file_put_contents("test.txt", $file);
?>