Solved

preg_match_all returns false for first match

Posted on 2010-11-28
6
378 Views
Last Modified: 2012-05-10
I am writing a calculator engine, and need to get the numbers and operators from the expression (a string). To do this I am using preg_match_all, with PREG_OFFSET_CAPTURE, that returns an array with all of the numbers and another with the operators. The operators part works fine, but the first matched number in the array is false instead of the number. Here is the function I am using:

$exp='13+4x2';
preg_match_all('/[0-9.\-\[]+/',$exp,$exp_nums,PREG_OFFSET_CAPTURE);

Open in new window


this returns:

array(1) {
  [0]=>
  array(3) {
    [0]=>
    array(2) {
      [0]=>
      bool(false) //improper value
      [1]=>
      int(1)
    }
    [1]=>
    array(2) {
      [0]=>
      string(1) "4"
      [1]=>
      int(4)
    }
    [2]=>
    array(2) {
      [0]=>
      string(1) "2"
      [1]=>
      int(6)
    }
  }
}

Open in new window


Does anyone know how to fix this
0
Comment
Question by:cloud-9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 34227650
How about this: (results below)

$exp='13+4x2';
preg_match_all('/[0-9.]+/','#'.$exp,$exp_nums,PREG_OFFSET_CAPTURE);
print_r($exp_nums);
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 13
                    [1] => 1
                )

            [1] => Array
                (
                    [0] => 4
                    [1] => 4
                )

            [2] => Array
                (
                    [0] => 2
                    [1] => 6
                )

        )

)

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34227655
Your code gives me this result actually, which seems different from what you're getting:
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 13
                    [1] => 0
                )

            [1] => Array
                (
                    [0] => 4
                    [1] => 3
                )

            [2] => Array
                (
                    [0] => 2
                    [1] => 5
                )

        )

)

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34227662
I'm not sure why you're trying to match - and [ characters in your pattern:
/[0-9.\-\[]+/

 but the backslashes need to have an extra escape character if you're going to try to do it:
/[0-9.\\-\\[]+/

However, with the code:
$exp='13+4x2-5-17';
preg_match_all('/[0-9.\\-\\[]+/',$exp,$exp_nums,PREG_OFFSET_CAPTURE);
print_r($exp_nums);

The result is probably not what you want:


Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 13
                    [1] => 0
                )

            [1] => Array
                (
                    [0] => 4
                    [1] => 3
                )

            [2] => Array
                (
                    [0] => 2-5-17
                    [1] => 5
                )

        )

)

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Terry Woods
ID: 34227663
(My first suggestion solves that problem)
0
 
LVL 3

Assisted Solution

by:cloud-9
cloud-9 earned 0 total points
ID: 34235119
The - and ] characters denote a negative sign (a subtraction operator is _) and a fraction symbol such as on a scientific calculator (ex 2[5 means two fifths). "2-5-17" would  produce a syntax error on my calculator. But all of this is beside the point.

I discovered my error: I am using gmp_init to convert all of the numbers in the array. I forgot I was using a different array structure previously, and I was trying to convert an array into a gmp number. Apparently the failure of the first gmp_init broke the loop and the other values were not processed. The array I was var_dumping came after the failed gmp_init. So there was no problem with preg_match_all in the first place. Sorry for inconvenience.
0
 
LVL 3

Author Closing Comment

by:cloud-9
ID: 34272890
I am still giving points for the prompt response.
0

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fillable forms on website 2 43
problems with widget background image 9 34
Securing source code... theoretical question 10 48
PHP processing webform 25 45
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question