donglian5309 2013-08-19 11:00
浏览 12

在php模板文件中搜索和替换

Basically trying to convert my whole zend framework website into multilingual. Initially there was no plan for it and view code was written without using a single wrapper function which we could use at the juncture. View code in all of the phtml files in my zf project looks like below:

<div class="contact">
  <span class="edit">
    <a href="javascript:void(0);">edit</a></span>
    <h2>Job Board Info</h2>
    <div class="info-grid">
      <div class="field">Contact Name: </div>
      <div class="field-info">
           <?php echo "Good Guy";?>
      </div>
    </div>
    <div class="info-grid">
     <div class="field">Description: </div>
     <div class="field-info">
      <?php echo "He is a very good guy";?>
     </div>
    </div>
</div>

I want to search all phtml files for the texts like edit, Contact Name, Good Guy, Description, He is avery good guy and replace them with

<? echo translate("edit") ?>
<? echo translate("Contact Name") ?>
translate("Good Guy");
<? echo translate("Description") ?>
translate("He is avery good guy");

I can perform regex search and replace in all phtml files using Zend Studio but I am not sure what would be the right regex pattern in order to achieve this. Above texts are not fixed in all tenmplates but is and example only. The regex should skip all html code and find out texts only and replace them with . if any text being echoed like echo "text" should be replaced with translate("text").

Or please suggest some other way in order to achieve this as fast as possible.

  • 写回答

1条回答 默认 最新

  • dtrz17917 2013-08-19 11:32
    关注

    What I would do is use for example TextCrawler (for search&replace multiple files at once) and simply turn

    • >edit<
    • >contact name: <
    • >Description: <

    with case-sensitivity off into

    • ><?php echo translate("edit"); ?><
    • ><?php echo translate("Contact Name"); ?><
    • ><?php echo translate("Description"); ?><

    (this depends on how many groups of words you want to translate. this is the manual way. If you have a bambillion of different things to 'process' like this you may want to use regex for this as well)

    ..and as for the already PHPfied text in echos use indeed regex:

    search for (.*)<.*echo.*["\'](.*)["\'].*> (you may want to be a bit more specific, as this can make alot of false positives - make sure it's suited for the content of the files you want to specifically 'process')

    and replace with $1<?php echo translate("$2"); ?>

    ..where $1 (first parentheses in the 'search for' condition) stands for whitespaces on the line or whatever (trying to keep the indentation) and $2 (second parentheses) stands for the originally echoed string. It also takes both "s and 's as correct quotation for echoed text.

    And for the love of science:

    BACKUP all your code!

    ..before you do things like this :D

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?