2

我是新来的,完全是新手。对不起,如果这个问题太基本了,但我想不出最好的方法来做到这一点。

我有很多包含各种文本占位符的 .php 文件。例如,在我的 .php 文件中,我会有这样的内容:

我非常喜欢 xCity,想住在那里。

或链接,

<a href="xcity_is_awesome.php">

我想我可以在每个页面的头部使用一些 php 代码来做一个 preg_replace,它将分别用“mycity”或“Mycity”替换“xcity”或“xCity”的任何实例。

我目前可以让它工作:

< ?php
 $xCity = "Mycity"; ?>

只要我将文档中所有出现的“xCity”替换为:

<?php echo $xCity ?>

我知道有一种更聪明的方法可以做到这一点,但就像我说的,我是全新的。

谁能帮我?


这是我现在正在尝试的:

使用下面的示例之一,我似乎无法让它工作。

我将文档中的几个变量从“xcity”更改为“{{xcity}}”。

这是我在 php 文件中的代码:

<?php
$xCITY = "MYCITY"; 
$xcity = "mycity";
$find    = array('{{xCITY}}','{{xcity}}');
$replace = array($xCity,$xcity);
$content = str_ireplace($find,$replace,$content);
?>

当我上传文件并预览它时,php 不会更改文本。我确定我一定做错了什么。:)


我目前正在使用 SpencerJ 的建议(如下):

<?php
 $xCity = "Calgary"; ?>

然后使用 echo 语句换出变量:

<?php echo $xCity ?>

我似乎遇到了 php 的问题,包括:

<?php include("<?php echo $xcity ?>_bottomlinks.php"); ?>

现在,我敢打赌,这样的多个 php“命令”相互之间存在问题。这将如何处理?

4

5 回答 5

0
$str = '<?php echo $xCity ?>';
echo str_replace('xCity', 'Mycity',$str);

输出-

<?php echo $Mycity ?>

检查 - http://codepad.org/uVs6w3kH

让我知道这是否是您要找的?

于 2013-06-21T17:29:46.240 回答
0

您可以使用输出控制功能

<?php
$xCity = "Mycity"; 
ob_start(); // tell php you want it to buffer the output, not send it to the browser yet
?>
Text containing xCity
and maybe other stuff to replace
<?php
// now the page is complete, get the contents (and empty the output buffer)
$out = ob_get_clean();
// replace all the strings you want replaced
...
// and send the output to the browser
echo $out;
?>

优点是您实际上并没有接触页面本身,您只需将它们包装在该输出缓冲区中。但从长远来看,使用适当的基于 php 的模板系统可能会更容易维护。

于 2013-06-21T17:30:00.903 回答
0

您应该能够在大多数 IDE 中执行查找和替换项目,以将 xCity 更改为<?php echo $xCity; ?>. 对于 prepending <?php $xCity = "Mycity"; ?>,您可能会在任何还允许正则表达式搜索的 IDE 中通过使用/^/s(或类似的东西)执行相同的操作,尽管仅在需要它的文件中手动添加它可能会更容易。

代替:

<div>My city is xCity and I think xCity is the coolest city in the world. Everyone should live in xCity!</div>

采用:

<?php $xCity = 'Calgary'; ?>
<div>My city is <?php echo $xCity; ?> and I think <?php echo $xCity; ?> is the coolest city in the world. Everyone should live in <?php echo $xCity; ?>!</div>
于 2013-06-21T17:31:29.037 回答
0

如果您要使用占位符,请使它们独一无二以给它们一个边界

例如:<a href="{{xcity}}_is_awesome.php">

这样您就可以保护变量,例如:

$xcity="";从被替换。

<?php
$find    = array('{{xcity}}','{{mycity}}');
$replace = array($xcity,$mycity);

$content = str_ireplace($find,$replace,$content);
?>

或者更好的方法是定义要在脚本中使用的实际 PHP 变量。

EG 在您的文件中:<a href="<?php echo $xcity ?>_is_awesome.php">然后在调用文件/视图时,您可以传递值。

<?php 
//define your values
$data['xcity'] = "Some Value";
//load view and pass your variables to it
$content = loadContentView('TheFile', $data);


function loadContentView($view, $data=null){
    $path = './path/to/the/files/'.$view.'.php';

    if (file_exists($path) === false){
        die('Content view not found: '.$path);
    }

    /*Extract the $data passed to this function*/
    if($data !== null){
        extract($data);
    }

    ob_start();
    require($path);
    $return = ob_get_contents();
    ob_end_clean();

    return $return;
}
?>

希望能帮助到你...

于 2013-06-21T17:43:45.883 回答
0
$webpage = file_get_contents('mypage.html');

$findme = array('xcity','mycity');
$replace = array('mycity','Mycity');

$webpage = str_replace($findme,$replace,$webpage);

echo $webpage;
于 2013-06-21T17:51:27.333 回答