【问题标题】:preg_replace to replace all instances of text in .php filepreg_replace 替换 .php 文件中的所有文本实例
【发布时间】:2013-06-21 17:24:32
【问题描述】:

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

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

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

或链接,

<a href="xcity_is_awesome.php">

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

我目前可以让它工作:

< ?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“命令”相互之间存在问题。这将如何处理?

【问题讨论】:

    标签: php replace preg-replace str-replace


    【解决方案1】:

    您应该能够在大多数 IDE 中执行 find-and-replace-in-project,以使用 &lt;?php echo $xCity; ?&gt; 更改 xCity。对于预先添加&lt;?php $xCity = "Mycity"; ?&gt;,您可能会在任何还允许正则表达式搜索的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>
    

    【讨论】:

    • 感谢您的回复!是的,我使用 BBedit 进行查找/替换,但我也在使用 Dreamweaver 模板。当/如果我更新模板文件,它将删除我替换的任何“变量”。我知道我可以在 DW 模板中将每个变量设为“可编辑”区域……但我确信有更好的方法吗?
    • 我对 DreamWeaver 模板没有任何经验,但可以将 PHP 代码直接插入模板,而不是占位符。
    • 是的,我就是这么做的。只是将代码放入模板中 :) 但是作为新手,我无法弄清楚编写代码的正确方法。
    • 没有看到 DW 输出什么,我想我能帮上什么忙。听起来它可能正在转义或剥离您的 PHP 代码,如果它没有正确显示在您的输出中。
    • 啊……我误会了。是的,我可以用 。但这是解决这个问题的最好方法吗?我的 html/php 标记中可能有很多回声实例。这很聪明吗? (我继承了这个网站,所以它是一个现有的网站......不是从头开始)
    【解决方案2】:
    $str = '<?php echo $xCity ?>';
    echo str_replace('xCity', 'Mycity',$str);
    

    输出 -

    <?php echo $Mycity ?>
    

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

    如果这是您要找的,请告诉我?

    【讨论】:

      【解决方案3】:

      您可以使用output control functions

      <?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 的模板系统可能会更容易维护。

      【讨论】:

        【解决方案4】:

        如果您要使用占位符,请将它们设为唯一以赋予它们边界

        例如:&lt;a href="{{xcity}}_is_awesome.php"&gt;

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

        $xcity=""; 被替换。

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

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

        文件中的 E.G:&lt;a href="&lt;?php echo $xcity ?&gt;_is_awesome.php"&gt; 然后在调用文件/视图时,您可以传递值。

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

        希望对你有帮助...

        【讨论】:

        • 使用你的第一个例子,我似乎无法让它工作。
          我将文档中的几个变量从“xcity”更改为“{{xcity}}”。这是我在 php 文件中的代码: 当我上传文件并预览它时,php 不会改变文本。我确定我一定做错了什么。 :)
        • @user2509823 哦,对了,用您尝试过的更新代码更新您的问题。
        • 啊。是的,我想这些评论部分不允许我发布代码的 sn-ps。更新了原始问题:)
        【解决方案5】:
        $webpage = file_get_contents('mypage.html');
        
        $findme = array('xcity','mycity');
        $replace = array('mycity','Mycity');
        
        $webpage = str_replace($findme,$replace,$webpage);
        
        echo $webpage;
        

        【讨论】:

        • 你知道这种方法是否会减慢网页的加载速度吗?好吧,放慢速度以影响 SEO 的加载时间?
        猜你喜欢
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-23
        • 1970-01-01
        • 2011-05-15
        • 2017-01-30
        • 2018-11-28
        相关资源
        最近更新 更多