2011-02-10 74 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

請問您能解釋一下您的問題的背景並提供一個例子嗎? – 2011-02-10 13:23:29

+0

你試過[`str_replace`](http://php.net/str_replace)嗎? – deceze 2011-02-10 13:24:10

回答

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

您可以創建與關鍵字匹配的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); 
     } 
    } 
} 
?> 
0

我覺得你只是想這樣,你不

<?php 

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

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

?> 
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 
"; 
    } 
} 
}}