합계구하고 싶습니다.

합계구하고 싶습니다.

QA

합계구하고 싶습니다.

본문

wr_1에

 

3*3+9

 

이런식으로 입력되어 있습니다.

 

이거를 계산할 수 있나요?

 

$a = sum($write[wr_1]);

echo "$a";

 

이런식인가요??

 

wr_1필드의 모든 합계아니구요,,

 

이 질문에 댓글 쓰기 :

답변 2

eval 함수 쓰면 되는데 입력값이 숫자와 연산자인지 유효성을 검사한 후 사용하시면 된다고 하네요. 보안에 취약하다고...

 

$test = "(7.2*6)-20";

 

function matheval($equation)
{
 $equation = preg_replace("/[^0-9+\-.*\/()%]/","",$equation);
    // fix percentage calcul when percentage value < 10
    $equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation);
    // calc percentage
    $equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation);
    // you could use str_replace on this next line
    // if you really, really want to fine-tune this equation
    $equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation);
    if ( $equation == "" )
    {
      $return = 0;
    }
    else
    {
      eval("\$return=" . $equation . ";" );
    }
    return $return;
}

 

$test1 = matheval($test);
echo $test1; 

만약 php로 출력할 필요 없이 자바스크립트로 출력해도 무관하다면,

<script>

var a = <?php echo $view['wr_1']; ?>

alert(a);

</script>
이런식으로도 가능합니다.
답변을 작성하시기 전에 로그인 해주세요.
전체 123,525 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT