Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6837561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:29:31+00:00 2026-05-26T23:29:31+00:00

For example I have a statement: $var = ‘2*2-3+8’; //variable type is string How

  • 0

For example I have a statement:

$var = '2*2-3+8'; //variable type is string

How to make it to be equal 9 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:29:31+00:00Added an answer on May 26, 2026 at 11:29 pm

    From this page, a very awesome (simple) calculation validation regular expression, written by Richard van Velzen. Once you have that, and it matches, you can rest assured that you can use eval over the string. Always make sure the input is validated before using eval!

    <?php
    $regex = '{
        \A        # the absolute beginning of the string
        \h*        # optional horizontal whitespace
        (        # start of group 1 (this is called recursively)
        (?:
            \(        # literal (
    
            \h*
            [-+]?        # optionally prefixed by + or -
            \h*
    
            # A number
            (?: \d* \. \d+ | \d+ \. \d* | \d+) (?: [eE] [+-]? \d+ )?
    
            (?:
                \h*
                [-+*/]        # an operator
                \h*
                (?1)        # recursive call to the first pattern.
            )?
    
            \h*
            \)        # closing )
    
            |        # or: just one number
    
            \h*
            [-+]?
            \h*
    
            (?: \d* \. \d+ | \d+ \. \d* | \d+) (?: [eE] [+-]? \d+ )?
        )
    
        # and the rest, of course.
        (?:
            \h*
            [-+*/]
            \h*
            (?1)
        )?
        )
        \h*
    
        \z        # the absolute ending of the string.
    }x';
    
    $var = '2*2-3+8';
    
    if( 0 !== preg_match( $regex, $var ) ) {
        $answer = eval( 'return ' . $var . ';' );
        echo $answer;
    }
    else {
        echo "Invalid calculation.";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is LINQ allowed to have spaces in the statement? An example statement: var
For example, if I have an echo statement, there's no guarantee that the browser
I have a question about using subqueries in an Update statement. My example: UPDATE
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Here's the problem. I ,for example,have a string 2500.Its converted from byte array into
I have this function, the input is the var cmd for example a dmesg
I have a LINQ Statement. It searches an XML file. (See example below) There
The Javascript timer event has this basic syntax: var t=setTimeout(javascript statement,milliseconds); I have this
For example I have class Class Cls: var 1 var 2 def func(self): ---
If I have a few UNION Statements as a contrived example: SELECT * FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.