Use PHP code to verify that a string of numbers is a method of connecting a digital string

Source: Internet
Author: User
This article mainly introduces the use of PHP to determine whether or not to multiply the number of strings of the example, the text through the example code introduced in very detailed, for everyone has a certain reference learning value, the need for friends below with a small series to learn.

Describe

With such a problem, given a number string A, you need to determine whether a is a hyphen number string, the definition of a hyphen string is that a number string can be split into several numbers, the following number (starting from the nth number) is the product of the preceding 2 digits.

Example

(1) ' 122 '---can be split into [1|2|2]

Result: 1x2=2 (tandem digit string)

(2) ' 1122242 '---can be split into [11|22|242]

Result: 11x22=242 (tandem digit string)

(3) ' 1224832256 '---can be split into [1|2|2|4|8| 32|256]

Result: 1x2=2,2x2=4,2x4=8,4x8=32,8x32=256 (tandem digit string)

(4) ' 234547898185239692 '---can be split into [23454|7898|185239692]

Result: 23454x7898=185239692 (tandem digit string)

(5) ' 113 ' can be disassembled [1|1|3]

Result: 1x1! = 3 (non-connected multiplication number string)

Code


/** * Determines if the string function * @param $strNum * @return bool **/function isexec ($strNum) {$str = (string) $strNum; for ($i = 0; $i &lt ; Strlen ($STR);  $i + +) {$k = 1;   for ($j = $i + 1; $j < strlen ($STR); $j + +) {$a _i = 0;   $b _i = $i + 1;   $p _i = $i + 1;   $m _i = $k + +;   $c _i = $b _i + $m _i;   $res = false;   $formula = [];    while (1) {$a = substr ($str, $a _i, $p _i);    $b = substr ($str, $b _i, $m _i);    $n = $a * $b;    $c = substr ($str, $c _i, strlen ($n)); Echo ' <br/> '. $a. '*' . $b. '=' . $n. ' and '. $c.    ' <br/> '; if ($c) {$formula [] = $a. '*' . $b. '=' .    $n;    if ($c = = = False | | $c = = = "") {break;     } if (Intval ($n) = = Intval ($c)) {$p _i = strlen ($b);     $m _i = strlen ($n);     $a _i = $b _i;     $b _i = $c _i;     $c _i = $b _i + $m _i;    $res = true;     } else {$res = false;    Break    }} if ($res = = = True) {Print_r ($formula);   return true; }//var_dump ($res).  ' <br/> '; }} return false;}

Perform

var_dump(isExec('1224832256'));


Running results array[   [0] = 1*2=2   [1] = 2*2=4   [2] = = 2*4=8   [3] = = 4*8=32   [4] = = 8*32=256] BOOL (TRUE)

var_dump(isExec('234547898185239692'));


Run result array[   [0] = 23454*7898=185239692]bool (True)

var_dump(isExec('1122242'));


Run result array[   [0] = 11*22=242]bool (True)

var_dump(isExec('11234'));


Run result bool (false)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.