Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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_String_Plugins_Joomla_Replace - Fatal编程技术网

用PHP代码替换字符串

用PHP代码替换字符串,php,string,plugins,joomla,replace,Php,String,Plugins,Joomla,Replace,我需要在字符串中找到一个字符串(“foo”,例如),然后用PHP代码替换它。可能吗 我正在创建一个Joomla插件。当我的文章有类似于“{foo}”的内容时,我将用include(require,或其他什么)替换它 我做了一些类似于: public function onContentBeforeDisplay($context, &$row, &$params, $page=0) { // $row->text has the article te

我需要在字符串中找到一个字符串(“foo”,例如),然后用PHP代码替换它。可能吗

我正在创建一个Joomla插件。当我的文章有类似于“{foo}”的内容时,我将用include(require,或其他什么)替换它

我做了一些类似于:

public function onContentBeforeDisplay($context, &$row, &$params, $page=0)
    {
        // $row->text has the article text ...
        if (preg_match('/{contact-form}/', $row->text))
        {
            require_once(dirname(__FILE__) . DS . 'tmpl' . DS . 'default.php');
        }
    }
但这段代码将在开头插入default.php。我想在{contact form}标记中替换它


谢谢。

您可以使用PHP函数“
substr\u replace()
”,其详细信息如下所示。 \n”

/*这两个示例将所有$var替换为“bob”*/
echo substr\u replace($var,'bob',0)。“
\n”; echo substr_replace($var,'bob',0,strlen($var))。“
\n”; /*在$var开头插入'bob'*/ echo substr\u replace($var,'bob',0,0)。“
\n”; /** *您的食物和搜索在这里 */ /*接下来的两个将$var中的“MNRPQR”替换为“bob”*/ 回波子序列替换($var,'bob',10,-1)。“
\n”; echo substr\u replace($var,'bob',-7,-1)。“
\n”; /*从$var中删除'MNRPQR'*/ 回波子序列替换($var',10,-1)。“
\n”; ?>
您可以创建一个与关键字匹配的PHP文件(如:foo.PHP),只需从文本中提取关键字并使用它包含该文件

例如:

<?php
$str = "This is {foo} text";
$includePath = "/path/to/my/files/";

// Careful with the Regular Expression. If you allow chars like . in the
// keyword this could create a security problem. (Dots allow you to go backwards
// which would allow people to execute files outside your path.)
if (preg_match_all('/\{([\w]+)\}/', $str, $matches))
{
    foreach ($matches[1] as $_match)
    {
        $filePath = $includePath . $_match . ".php";
        if (file_exists($filePath))
        {
            include $filePath;
        }
        else
        {
            trigger_error("File does not exist '$filePath'.", E_USER_WARNING);
        }
    }
}
?>

我想你只是想要这个,不是吗

<?php

$replacement = file_get_contents('myfile.php');

$content = str_replace('{foo}', eval($replacement), $content);

?>

如果您需要替换远程服务器上的一系列文件中的任何字符串,而且您没有时间,那么这里是您的解决方案,我希望它可以帮助其他人

 ////
 //PHP 5.3 + Class find and replace string in files
 //
 //by Bruce Afruz 
 //
 //2013
 //
 //example usage for single file:
 //
 //$new = new fileReplacement('./');
 //$new->setExt("check.php");
 //$new->changeContents("hello", "goodbye");
 //
 //example usage for multiple files:
 //
 //$new = new fileReplacement('./test');
 //$new->setExt("*.html");
 //$new->changeContents("hello", "goodbye");
 //
 //to change directory:
 //
 //$new = new fileReplacement('./test');
 //$new->setDir("./test2");
 //$new->setExt("*.html");
 //$new->changeContents("hello", "goodbye");
 ////


 class fileReplacement 
 {
  private $ext , $dir ;
  public function getDir() {
   return $this->dir;
  }
  public function setDir($dir) {
   $this->dir = $dir;
  }
   public function getExt() {
   return $this->ext;
  }
  public function setExt($ext) {
   $this->ext = $ext;
  }
 function __construct($dir) {
   $this->dir = $dir;
  }

  public function rglob($pattern = '*', $flags = 0, $path = '') {

  chdir($this->getDir());
  $paths = glob($path . '*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
  $files = glob($path . $pattern, $flags);
  foreach ($paths as $path) {
  $files = array_merge($files, $this->rglob($pattern, $flags, $path));
  }
  return $files;
 }

 public function changeContents($replace , $sentence , $flags = 0, $path = '') {
 $all = $this->rglob($this->getExt() , $flags, $path);
 foreach ($all as $file) {

  $filename = $file;
  $handle = fopen($filename, "r");
  $contents = fread($handle, filesize($filename));
  fclose($handle);
  $contents = str_replace($replace , $sentence, $contents);

  if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'w+')) {
    echo "Cannot open file ($filename)
";
    exit;
   }

   // Write $contents to our opened file.
   if (fwrite($handle, $contents) === FALSE) {
    echo "Cannot write to file ($filename)
";
    exit;
   }

   echo "Success, wrote content to file ($filename)
";

   fclose($handle);
  } else {
   echo "The file $filename is not writable
";
  }
 }
 }}

你能解释一下你的问题的背景并提供一个例子吗?你试过了吗?@deceze这个问题被标记为str replace,你几乎会认为OP首先检查了文档……不是吗?;-)str_replace添加了代码而没有评估它。@rich Good point.:o)@thom你为什么不先运行代码并替换
{foo}
运行代码的结果是什么?
 ////
 //PHP 5.3 + Class find and replace string in files
 //
 //by Bruce Afruz 
 //
 //2013
 //
 //example usage for single file:
 //
 //$new = new fileReplacement('./');
 //$new->setExt("check.php");
 //$new->changeContents("hello", "goodbye");
 //
 //example usage for multiple files:
 //
 //$new = new fileReplacement('./test');
 //$new->setExt("*.html");
 //$new->changeContents("hello", "goodbye");
 //
 //to change directory:
 //
 //$new = new fileReplacement('./test');
 //$new->setDir("./test2");
 //$new->setExt("*.html");
 //$new->changeContents("hello", "goodbye");
 ////


 class fileReplacement 
 {
  private $ext , $dir ;
  public function getDir() {
   return $this->dir;
  }
  public function setDir($dir) {
   $this->dir = $dir;
  }
   public function getExt() {
   return $this->ext;
  }
  public function setExt($ext) {
   $this->ext = $ext;
  }
 function __construct($dir) {
   $this->dir = $dir;
  }

  public function rglob($pattern = '*', $flags = 0, $path = '') {

  chdir($this->getDir());
  $paths = glob($path . '*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
  $files = glob($path . $pattern, $flags);
  foreach ($paths as $path) {
  $files = array_merge($files, $this->rglob($pattern, $flags, $path));
  }
  return $files;
 }

 public function changeContents($replace , $sentence , $flags = 0, $path = '') {
 $all = $this->rglob($this->getExt() , $flags, $path);
 foreach ($all as $file) {

  $filename = $file;
  $handle = fopen($filename, "r");
  $contents = fread($handle, filesize($filename));
  fclose($handle);
  $contents = str_replace($replace , $sentence, $contents);

  if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'w+')) {
    echo "Cannot open file ($filename)
";
    exit;
   }

   // Write $contents to our opened file.
   if (fwrite($handle, $contents) === FALSE) {
    echo "Cannot write to file ($filename)
";
    exit;
   }

   echo "Success, wrote content to file ($filename)
";

   fclose($handle);
  } else {
   echo "The file $filename is not writable
";
  }
 }
 }}