$val="wqewq<div id='sdf hjhj'></div>qwewqewqewq"
替换成"wqewq<div id='sdf_test hjhj_test'></div>qwewqewqewq"
//其实也就是在class,id值后面加上一个字符串
//我要替换的是文件里的。所以要从文件读取出来
//我自己写了一个但是不行类似这样$fp=fopen("tpl_checkout_stacked.php","rb");
$content=fread($fp,filesize("tpl_checkout_stacked.php"));
$content=ereg_replace(" id='(.*)'"," id='\\1_test'",$content);
$content=ereg_replace(" class='(.*)'"," class='\\1_test'",$content);
echo($content);
fclose($fp);
$fp=fopen("test.php","a");
fwrite($content,$fp);

解决方案 »

  1.   

    $filepath = '/path/to/file';
    $content = file_get_contents($filepath);
    替换
    file_put_contents($filepath, $content);
      

  2.   

    // +------ 我以前写的一个替换函数function file_modify($search_contents, $replace_contents, $filename)
    {
            $fp = file_get_contents($filename);
            $new_fp = str_replace($search_contents, $replace_contents, $fp);
            file_put_contents($filename, $new_fp);
    }// +------ 用法
    file_modify('sdf hjhj', 'sdf_test hjhj_test', 'test10.html');
    能运行吧 
      

  3.   

    PHP中可以使用正则替换。
    如果只是为了满足页面显示效果,可以不用改动PHP代码,在页面中加入JS代码,使用document对象操作即可。
      

  4.   

    preg_replace("/(class='([^\s]+)\s+([^']+)/gi","class='$1_test $2_test'",$content);
    preg_replace("/id='([^\s]+)\s+([^']+)/","id='$1_test $2_test'",$content);
      

  5.   

    <?php
    $html="wqewq<div id='sdf hjhj'></div>qwewqewqewq";
    $html = preg_replace("/class='([^\s]+)\s+([^']+)/i","class='$1_test $2_test",$html);
    $html = preg_replace("/id='([^\s]+)\s+([^']+)/i","id='$1_test $2_test",$html);
    echo $html;?>
    验证的时候要看网页源代码才知道,div被浏览器解释执行了。
      

  6.   

    上面的那位大哥。你可能理解错了<div id='sdf hjhj'>这个东西是id只有一个值而class可能是<div class="sd">或者是<div class="sd df">而这些值都要加一个固定的值变成<div class="sd_test">,<div class="sd_test df_test">,<div id='sdf_test'>
      

  7.   

    例如文件的内容是:
    <?php
    /**
     * Common Template - tpl_main_page.php
     *
     * Governs the overall layout of an entire page<br />
     * Normally consisting of a header, left side column. center column. right side column and footer<br />
     * For customizing, this file can be copied to /templates/your_template_dir/pagename<br />
     * example: to override the privacy page<br />
     * - make a directory /templates/my_template/privacy<br />
     * - copy /templates/templates_defaults/common/tpl_main_page.php to /templates/my_template/privacy/tpl_main_page.php<br />
     * <br />
     * to override the global settings and turn off columns un-comment the lines below for the correct column to turn off<br />
     * to turn off the header and/or footer uncomment the lines below<br />
     * Note: header can be disabled in the tpl_header.php<br />
     * Note: footer can be disabled in the tpl_footer.php<br />
     * <br />
     * $flag_disable_header = true;<br />
     * $flag_disable_left = true;<br />
     * $flag_disable_right = true;<br />
     * $flag_disable_footer = true;<br />
     * <br />
     * // example to not display right column on main page when Always Show Categories is OFF<br />
     * <br />
     * if ($current_page_base == 'index' and $cPath == '') {<br />
     *  $flag_disable_right = true;<br />
     * }<br />
     * <br />
     * example to not display right column on main page when Always Show Categories is ON and set to categories_id 3<br />
     * <br />
     * if ($current_page_base == 'index' and $cPath == '' or $cPath == '3') {<br />
     *  $flag_disable_right = true;<br />
     * }<br />
     *
     * @package templateSystem
     * @copyright Copyright 2003-2007 Zen Cart Development Team
     * @copyright Portions Copyright 2003 osCommerce
     * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
     * @version $Id: tpl_main_page.php 7085 2007-09-22 04:56:31Z ajeh $
     */// the following IF statement can be duplicated/modified as needed to set additional flags
      if (in_array($current_page_base,explode(",",'list_pages_to_skip_all_right_sideboxes_on_here,separated_by_commas,and_no_spaces')) ) {
        $flag_disable_right = true;
      }
      $header_template = 'tpl_header.php';
      $footer_template = 'tpl_footer.php';
      $left_column_file = 'column_left.php';
      $right_column_file = 'column_right.php';
      $body_id = ($this_is_home_page) ? 'indexHome' : str_replace('_', '', $_GET['main_page']);
    ?>
    <body id="<?php echo $body_id . 'Body'; ?>"<?php if($zv_onload !='') echo ' onload="'.$zv_onload.'"'; ?>>
    <?php
      if (SHOW_BANNERS_GROUP_SET1 != '' && $banner = zen_banner_exists('dynamic', SHOW_BANNERS_GROUP_SET1)) {
        if ($banner->RecordCount() > 0) {
    ?>
    <div id="bannerOne" class="banners"><?php echo zen_display_banner('static', $banner); ?></div>
    <?php
        }
      }
    ?><div id="mainWrapper">
    <?php
     /**
      * prepares and displays header output
      *
      */
      if (CUSTOMERS_APPROVAL_AUTHORIZATION == 1 && CUSTOMERS_AUTHORIZATION_HEADER_OFF == 'true' and ($_SESSION['customers_authorization'] != 0 or $_SESSION['customer_id'] == '')) {
        $flag_disable_header = true;
      }
      require($template->get_template_dir('tpl_header.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/tpl_header.php');?><table width="100%" border="0" cellspacing="0" cellpadding="0" id="contentMainWrapper">
      <tr>
    <?php
    if (COLUMN_LEFT_STATUS == 0 || (CUSTOMERS_APPROVAL == '1' and $_SESSION['customer_id'] == '') || (CUSTOMERS_APPROVAL_AUTHORIZATION == 1 && CUSTOMERS_AUTHORIZATION_COLUMN_LEFT_OFF == 'true' and ($_SESSION['customers_authorization'] != 0 or $_SESSION['customer_id'] == ''))) {
      // global disable of column_left
      $flag_disable_left = true;
    }
    if (!isset($flag_disable_left) || !$flag_disable_left) {
    ?> <td id="navColumnOne" class="columnLeft" style="width: <?php echo COLUMN_WIDTH_LEFT; ?>">
    <?php
     /**
      * prepares and displays left column sideboxes
      *
      */
    ?>
    <div id="navColumnOneWrapper" style="width: <?php echo BOX_WIDTH_LEFT; ?>"><?php require(DIR_WS_MODULES . zen_get_module_directory('column_left.php')); ?></div></td>
    <?php
    }
    ?>
        <td valign="top">
    <!-- bof  breadcrumb -->
    <?php if (DEFINE_BREADCRUMB_STATUS == '1' || (DEFINE_BREADCRUMB_STATUS == '2' && !$this_is_home_page) ) { ?>
        <div id="navBreadCrumb"><?php echo $breadcrumb->trail(BREAD_CRUMBS_SEPARATOR); ?></div>
    <?php } ?>
    <!-- eof breadcrumb --><?php
      if (SHOW_BANNERS_GROUP_SET3 != '' && $banner = zen_banner_exists('dynamic', SHOW_BANNERS_GROUP_SET3)) {
        if ($banner->RecordCount() > 0) {
    ?>
    <div id="bannerThree" class="banners"><?php echo zen_display_banner('static', $banner); ?></div>
    <?php
        }
      }
    ?><!-- bof upload alerts -->
    <?php if ($messageStack->size('upload') > 0) echo $messageStack->output('upload'); ?>
    <!-- eof upload alerts --><?php
     /**
      * prepares and displays center column
      *
      */
     require($body_code); ?><?php
      if (SHOW_BANNERS_GROUP_SET4 != '' && $banner = zen_banner_exists('dynamic', SHOW_BANNERS_GROUP_SET4)) {
        if ($banner->RecordCount() > 0) {
    ?>
    <div id="bannerFour" class="banners"><?php echo zen_display_banner('static', $banner); ?></div>
    <?php
        }
      }
    ?></td><?php
    //if (COLUMN_RIGHT_STATUS == 0 || (CUSTOMERS_APPROVAL == '1' and $_SESSION['customer_id'] == '') || (CUSTOMERS_APPROVAL_AUTHORIZATION == 1 && CUSTOMERS_AUTHORIZATION_COLUMN_RIGHT_OFF == 'true' && $_SESSION['customers_authorization'] != 0)) {
    if (COLUMN_RIGHT_STATUS == 0 || (CUSTOMERS_APPROVAL == '1' and $_SESSION['customer_id'] == '') || (CUSTOMERS_APPROVAL_AUTHORIZATION == 1 && CUSTOMERS_AUTHORIZATION_COLUMN_RIGHT_OFF == 'true' and ($_SESSION['customers_authorization'] != 0 or $_SESSION['customer_id'] == ''))) {
      // global disable of column_right
      $flag_disable_right = true;
    }
    if (!isset($flag_disable_right) || !$flag_disable_right) {
    ?>
    <td id="navColumnTwo" class="columnRight" style="width: <?php echo COLUMN_WIDTH_RIGHT; ?>">
    <?php
     /**
      * prepares and displays right column sideboxes
      *
      */
    ?>
    <div id="navColumnTwoWrapper" style="width: <?php echo BOX_WIDTH_RIGHT; ?>"><?php require(DIR_WS_MODULES . zen_get_module_directory('column_right.php')); ?></div></td>
    <?php
    }
    ?>
      </tr>
    </table><?php
     /**
      * prepares and displays footer output
      *
      */
      if (CUSTOMERS_APPROVAL_AUTHORIZATION == 1 && CUSTOMERS_AUTHORIZATION_FOOTER_OFF == 'true' and ($_SESSION['customers_authorization'] != 0 or $_SESSION['customer_id'] == '')) {
        $flag_disable_footer = true;
      }
      require($template->get_template_dir('tpl_footer.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/tpl_footer.php');
    ?></div>
    <!--bof- parse time display -->
    <?php
      if (DISPLAY_PAGE_PARSE_TIME == 'true') {
    ?>
    <div class="smallText center">Parse Time: <?php echo $parse_time; ?> - Number of Queries: <?php echo $db->queryCount(); ?> - Query Time: <?php echo $db->queryTime(); ?></div>
    <?php
      }
    ?>
    <!--eof- parse time display -->
    <!--bof- banner #6 display -->
    <?php
      if (SHOW_BANNERS_GROUP_SET6 != '' && $banner = zen_banner_exists('dynamic', SHOW_BANNERS_GROUP_SET6)) {
        if ($banner->RecordCount() > 0) {
    ?>
    <div id="bannerSix" class="banners"><?php echo zen_display_banner('static', $banner); ?></div>
    <?php
        }
      }
    ?>
    <!--eof- banner #6 display -->
    </body>
    里面的所有ID,CLASS的值都要在原来的基础上加上_test这样
    如class="columnRight" 变成class="columnRight_test" 
      

  8.   

    要用非贪婪匹配才可以, 用PCRE吧,POSIX Regex不好
      

  9.   

    你这里都没说清楚,<div id='sdf hjhj'>,这里div的id不就有两个值吗?根据<div id='sdf_test'>,难道是id属性空格后面的不要了?
      

  10.   

    郁闷,不用这样做了。不过大家研究一下也可以的!其实就是在id和class的值加一个后缀啊。一个下划线和字母例如_domain。还不明白吗?
      

  11.   

    随便在哪个IDE里写个正则就可以换 了