如何使用file_get_contents()获取preg_replace来替换html中的双支撑分隔符?

如何使用file_get_contents()获取preg_replace来替换html中的双支撑分隔符?

问题描述:

I am able to get the preg_replace function to find and replace only the inner braced delimiter. eg: the script below replaces { FIELD: date } with "12/12/2015" but leaves the outer braces.

I tried escaping the outer braces to no avail. Help please!

$myFileToCopy = file_get_contents("http://localhost/templatemaker/masterTemplates/".$object['fileName'].$object['fileExtension']);


$myFileToCopy =  preg_replace("{{ FIELD: date }}", "12/12/2015", $myFileToCopy);

我能够获取preg_replace函数来查找和替换内部支撑定界符。 例如:下面的脚本用“12/12/2015” code>替换 {FIELD:date} code>,但留下外括号。 p>

我试图逃避外括号无济于事。 请帮忙! p>

  $ myFileToCopy = file_get_contents(“http:// localhost / templatemaker / masterTemplates /”。$ object ['fileName']。$ object ['fileExtension']  ); 
 
 
 $ myFileToCopy = preg_replace(“{{FIELD:date}}”,“12/12/2015”,$ myFileToCopy); 
  code>  pre> 
  div  >

You aren't in need of a regex here given your example so str_replace, http://php.net/str_replace, should suffice.

$myFileToCopy = file_get_contents("http://localhost/templatemaker/masterTemplates/".$object['fileName'].$object['fileExtension']);
$myFileToCopy = str_replace("{{ FIELD: date }}", "12/12/2015", $myFileToCopy);

If you wanted to do it with a regex you could try,

$myFileToCopy = file_get_contents("http://localhost/templatemaker/masterTemplates/".$object['fileName'].$object['fileExtension']);
$myFileToCopy =  preg_replace("~\{\{ FIELD: date \}\}~", "12/12/2015", $myFileToCopy);

Note the ~, these are delimiters, http://php.net/manual/en/regexp.reference.delimiters.php, which lets PHP know where the expression starts and ends.