2011-02-10 10 views
0

文字列(たとえば "foo")内の文字列を見つけて、それをPHPコードに置き換える必要があります。出来ますか?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}タグに置き換えたいと思います。

ありがとうございます。

+2

問題のコンテキストを説明して例を挙げてください。 –

+0

['str_replace'](http://php.net/str_replace)を試しましたか? – deceze

+0

@deceze質問はstr-replaceとタグ付けされていましたが、あなたはほとんどOP *がドキュメントを最初にチェックしたと思います。 ;-) – richsage

答えて

0

PHP関数 "substr_replace()"(詳細はgiven here)を使用することができます。 N \ ";

/* These two examples replace all of $var with 'bob'. */ 
echo substr_replace($var, 'bob', 0) . "<br />\n"; 
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n"; 

/* Insert 'bob' right at the beginning of $var. */ 
echo substr_replace($var, 'bob', 0, 0) . "<br />\n"; 

/** 
* Your food & search is here 
*/ 
/* These next two replace 'MNRPQR' in $var with 'bob'. */ 
echo substr_replace($var, 'bob', 10, -1) . "<br />\n"; 
echo substr_replace($var, 'bob', -7, -1) . "<br />\n"; 

/* Delete 'MNRPQR' from $var. */ 
echo substr_replace($var, '', 10, -1) . "<br />\n"; 
?> 
0

あなたがキーワード(のような:foo.php)に一致する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); 
     } 
    } 
} 
?> 
0

私はあなたが、あなただけのこれをしたくないと思う

<?php 

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

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

?> 
+0

いいえ、私はそれを望んでいません。 – thom

+1

まあ...大丈夫、十分に....:S – simon

0

あなたがする必要がある場合は?あなたのリモートサーバー上の一連のファイルの任意の文字列を置き換えると、それは時間がありません! 、ここにあなたの解決策です、私はそれが誰かを助けることができると思います。

//// 
//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 
"; 
    } 
} 
}} 
関連する問題