Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.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_Regex_Preg Replace_Preg Match - Fatal编程技术网

如何在PHP中动态替换符号之间的文本并替换为常量?

如何在PHP中动态替换符号之间的文本并替换为常量?,php,regex,preg-replace,preg-match,Php,Regex,Preg Replace,Preg Match,我有一个问题,需要搜索HTML页面/代码段,替换四个百分位符号之间的任何值,并转换为常量变量,例如%%THIS_constant%%成为THIS_constant 现在我正在一行一行地搜索页面,通过使用preg_match_all和preg_replace可以找到匹配项并替换它们 $file_scan = fopen($directory.$file, "r"); if ($file_scan) { while

我有一个问题,需要搜索HTML页面/代码段,替换四个百分位符号之间的任何值,并转换为常量变量,例如%%THIS_constant%%成为THIS_constant

现在我正在一行一行地搜索页面,通过使用preg_match_all和preg_replace可以找到匹配项并替换它们

$file_scan = fopen($directory.$file, "r");    
if ($file_scan) {                                     
  while (($line = fgets($file_scan)) !== false) {
    if(preg_match_all('/\%%(.*?)\%%/', $line, $matches)){
      foreach($matches as $match){
        foreach($match as $m){
          $repair = preg_replace('/\%%(.*?)\%%/', $m, $m);
          if(preg_match('/\%%(.*?)\%%/', $m, $m)){

          } else {
            echo $repair.' '.$j;
            $j++;
          }
        }
        $lines[$i] = preg_replace('/\%%(.*?)\%%/', constant($repair), $line);
      }
    } else {
      $lines[$i] = $line;    
    }
    $i++; 
  }
  $template[$name] = implode("", $lines);
  fclose($file_scan);
}
这段代码无法在一行中查找和替换多个匹配项。例如,如果有一行带有:

<img src="%%LOGO_IMAGE%%"><h1>%%TITLE%%</h1>
我的最后一个问题是将替换的项更改为常量。可能吗

最终编辑:

在Peter Bowers建议的帮助下,我使用preg_replace_callback添加了将关键字更改为常量的功能:

foreach($filenames as $file){
  $name = str_replace('.html', '', $file);
  $template[$name] = preg_replace_callback('/\%%(.*?)\%%/', function($matches){                 
    $matches[0] = preg_replace('/\%%(.*?)\%%/', '$1', $matches[0]);
    return constant($matches[0]);
   }, file_get_contents($directory.$file));
  }
return $template;

这里有一个更简单的实现

$file_scan = fopen($directory.$file, "r");    
if ($file_scan) {                                     
  $out = '';
  while (($line = fgets($file_scan)) !== false) {
    $out .= preg_replace('/\%%(.*?)\%%/', '$1', $line);
    $i++; 
  }
  $template[$name] = $out;
  fclose($file_scan);
}
或者更简单一点:

$str = file_get_contents($directory.$file);
$template[$name] = preg_replace('/\%%(.*?)\%%/', '$1', $str);
而且,因为我们在这里做的很简单

$template[$name] = preg_replace('/\%%(.*?)\%%/', '$1', file_get_contents($directory.$file));
(很明显,当我们接近一行程序时,您正在失去一些错误检查功能,但是-嘿-我玩得很开心…:-)

尝试以下方法:

$file_scan = fopen($directory.$file, "r");
if ($file_scan) {
  while (($line = fgets($file_scan)) !== false) {
    $line = preg_replace('/\%%(.*?)\%%/', '$2'.'$1', $line);
    echo $line;
  }             
fclose($file_scan);
<?php

define('TITLE', 'Title');
define('LOGO_IMAGE', 'Image');

$lines = array();

$file_scan = fopen($directory.$file, "r");    
if ($file_scan) {                                     
  while (($line = fgets($file_scan)) !== false) {
    if(preg_match_all('/\%%(.*?)\%%/', $line, $matches)){
      for($i = 0; $i < count($matches[0]); $i++) {
          $line = str_replace($matches[0][$i], constant($matches[1][$i]), $line);
     }
     $lines[] = $line; 
     print_r($line);
   }
 }
}    
$template[$name] = implode("", $lines);
fclose($file_scan);

?>

删除所有代码,查看关于
preg_replace
的php手册,尝试几次以更好地理解其工作原理(使用文档示例并使用它们)。你会很快找到解决办法。别碰你的花样,你有好花样。