【问题标题】:Use gulp-replace-task to use replace in a php file (index.php)使用 gulp-replace-task 在 php 文件 (index.php) 中使用 replace
【发布时间】:2015-12-01 19:29:56
【问题描述】:

我正在尝试使用 gulp-replace-task 替换 index.php 文件中的文本。我知道这可以在 .html、.css 和 .js 文件中完成,但似乎无法在 .php 文件中使用。

有人可以帮帮我吗?

这是我的代码:

gulp.task('header', function() {

return gulp.src(rootPath + 'templates/header.php')

    .pipe(replace({
        patterns: [{
            json: {
                'main.css': 'style.css'
            }
        }]
    }))
    // Save the hashed css file
    .pipe(gulp.dest(rootPath + 'templates/header.php'));
});

【问题讨论】:

    标签: gulp gulp-rev gulp-replace


    【解决方案1】:

    不要使用“json”对象来查找/替换,而是使用下面 gulpfile.js 中的 RegEx 语法。您最终会得到包含替换文本的 build/php/templates/header.php:

    <?php
        $color = "blue";
    ?>
    <head>
        <link rel="stylesheet" src="style.css">
    </head>
    <body>
        Your color is <?php echo $color; ?>
    <body>
    

    gulpfile.js

    var gulp = require('gulp')
    var replace = require('gulp-replace-task')
    
    var config = {
       src : 'src/php/**/*',
       build: 'build/php'
    }
    
    gulp.task('build', function(cb) {
       gulp.src(config.src)
          .pipe(replace({
             patterns: [
                {
                   match: /COLOR/,
                   replacement: 'blue'
                },
                {
                   match: /main.css/,
                   replacement: 'style.css'
                }
             ]
          }))
          .pipe(gulp.dest(config.build))
    
       cb()
    })
    
    gulp.task('watch', function(cb) {
       gulp.watch(config.src, ['build']) 
    })
    
    gulp.task('default', ['build', 'watch'])
    

    header.php

    <?php
        $color = "COLOR";
    ?>
    <head>
        <link rel="stylesheet" src="main.css">
    </head>
    <body>
        Your color is <?php echo $color; ?>
    <body>
    

    项目文件结构

    $ tree -I node_modules
    .
    ├── build
    │   └── php
    │       └── templates
    │           └── header.php
    ├── gulpfile.js
    ├── package.json
    └── src
        └── php
            └── templates
                └── header.php
    
    6 directories, 4 files
    

    【讨论】:

      猜你喜欢
      • 2015-09-08
      • 2015-12-17
      • 2016-03-22
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      相关资源
      最近更新 更多