2016-12-09 1 views
1

私は検索と置換を行うときに私のビュー内のすべての変数の周りにエスケープをラップする正規表現を作成しようとしています。正規表現はすぐに変数をエスケープするために置き換えます

現在のコード

echo $this->cust['id']; 
echo $this->cust['firstname']; 
echo $this->cust['lastname']; 
echo $this->cust['postCode']; 

は、異なるビュー

の中の$ this - >引用符またはます$ this->オーダーされるように

echo $this->escape($this->cust['id']); 
echo $this->escape($this->cust['firstname']); 
echo $this->escape($this->cust['lastname']); 
echo $this->escape($this->cust['postCode']); 

ます$ this->カストが一貫していないになるべき

これは可能なのですか?もしそうなら、どのようにすることができますか?

+0

それで、 '$ this->'で始まるものはどれですか?あなたのコードを変更するスクリプトを書こうとしていますか? – varlogtim

+0

ええ、また交換したい];と]);各行の末尾 – code9monkey

+0

PhpStorm IDEのStructural Searchを使用すると、これはかなり簡単になると思います。万が一それを使用しますか? –

答えて

0

あなたはこれまで純粋なPHPソリューションをしたい場合は、[実行preg_replace

/tmp/current.php

echo "some other code"; 
echo $this->cust['id']; 
echo $this->cust['firstname']; 
echo $this->cust['lastname']; 
echo $this->cust['postCode']; 
echo $this->order['size']; 
function x() { echo $this->anything['derp']; } 

/tmp/regex.php

<?php 
$ifile = '/tmp/current.php'; 
$ofile = '/tmp/new_current.php'; 

$ifh = fopen($ifile, "r"); 
$ofh = fopen($ofile, "w"); 

$regex = '#(\$this->[^]]+])#'; 
$replace = '$this->escape($1)'; 

while(($line = fgets($ifh)) !== false) { 
    if($new_line = preg_replace($regex, $replace, $line)) { 
     fwrite($ofh, $new_line); 
    } 
    else fwrite($ofh, $line); 
} 
?> 

を使用することができます。 /tmp/regex.php、yeilds:

echo "some other code"; 
echo $this->escape($this->cust['id']); 
echo $this->escape($this->cust['firstname']); 
echo $this->escape($this->cust['lastname']); 
echo $this->escape($this->cust['postCode']); 
echo $this->escape($this->order['size']); 
function x() { echo $this->escape($this->anything['derp']); } 
関連する問題