I have clients you enter bedrooms as 1 + 2 instead of entering the overall total which in this case would be 3. There is method in PHP that will allow me to take a simple string and generate the sum value?
$bedrooms = '1 + 2';
I have clients you enter bedrooms as 1 + 2 instead of entering the overall total which in this case would be 3. There is method in PHP that will allow me to take a simple string and generate the sum value?
$bedrooms = '1 + 2';
 
    
    You could do something like this:
$bedrooms = array_sum(explode("+", str_replace(" ", "", "1 + 2")));
 
    
    Look up eval - That should do the trick.
But ensure that you have a good regular expression to ensure that the input will not do any harm
e.g. `^[1-9]+ *(+ [1-9]+)$'
 
    
    try this
    $bedrooms = 1 + 2;
    $pattern = '/([a-zA-Z \+\-])+/';
    $replacement = '+';
    echo array_sum(explode('+', preg_replace($pattern, $replacement, $bedrooms)));
just to make sure some characters are in the input string
