当我进行查找和替换时,我正在尝试创建一个正则表达式来包围我视图中所有变量的转义。
当前代码
var methodUrl = CombineUrl("http://something.com", "/task/status/", "dfgd/", "/111", "qqq");
应该成为
echo $this->cust['id'];
echo $this->cust['firstname'];
echo $this->cust['lastname'];
echo $this->cust['postCode'];
$ this-> cust不一致,因为它可能是$ this-> quote或$ this->订单在不同的视图中
这可能吗?如果可以的话怎么办呢?
答案 0 :(得分:1)
如果您想要一个纯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']); }