PHP过滤并替换字符串中的特定单词

时间:2015-10-14 07:51:21

标签: php

原始字符串

ROUND(({NOPAY_COUNT} * ({TOTAL_BASIC_SALARY}/24)),2)

我想过滤'{'& '}'用'$'替换单词

ROUND(($NOPAY_COUNT * ($TOTAL_BASIC_SALARY/24)),2)

3 个答案:

答案 0 :(得分:1)

您可以使用preg_replace()一次:

<?php
$string = 'ROUND(({NOPAY_COUNT} * ({TOTAL_BASIC_SALARY}/24)),2)';
$string = preg_replace("/{(.+?)}/", "\$$1", $string);
echo $string; //ROUND(($NOPAY_COUNT * ($TOTAL_BASIC_SALARY/24)),2)

看到你自己:http://www.phpliveregex.com/p/ddG

答案 1 :(得分:0)

您可以使用javascript两次:

preg_replace()

答案 2 :(得分:0)

  $NOPAY_COUNT = 1;
  $TOTAL_BASIC_SALARY = 1;
  $str = "ROUND(({NOPAY_COUNT} * ({TOTAL_BASIC_SALARY}/24)),2);";
  $str = str_replace("{", "$" , $str);
  $str = str_replace("}", "" , $str);
  eval("echo ".$str);