


How to determine whether a string of numbers is multiplied consecutively in PHP
There is such a question. Given a number string A, you need to determine whether A is a continuous multiplication number string. The definition of a continuous multiplication number string is that a number string can be split into several numbers. The following numbers (from the Nth starting with a number) is the product of the previous 2 numbers. This article mainly introduces you to an example of how to use PHP to determine whether it is a continuous multiplication of a number string. The article introduces it in detail through the example code, which has a certain reference and learning value for everyone. Friends who need it can follow the editor to learn together.
(1) '122' --- can be split into [1|2|2]
Result: 1×2=2 (multiply the number string)
(2) '1122242' --- can be split into [11|22|242]
Result: 11×22=242 (multiply the number string)
(3) '1224832256' --- can be split into [1|2|2|4|8|32|256]
Result: 1×2=2, 2×2=4, 2×4=8, 4×8=32, 8×32=256 (multiply the number string)
(4) '234547898185239692' --- can be split into [23454|7898|185239692]
Result: 23454×7898=185239692 (multiply the number string)
(5) '113' can be split into [1| 1|3]
Result: 1×1 != 3 (non-consecutive multiplication of numeric strings)
Code
/** * 判断是否连乘数字串函数 * @param $strNum * @return bool **/ function isExec($strNum) { $str = (string)$strNum; for ($i = 0; $i ' . $a . '*' . $b . '=' . $n . '->' . $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; }
Execution
var_dump(isExec('1224832256'));
//运行结果 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'));
//运行结果 Array [ [0] => 23454*7898=185239692 ] bool(true)
var_dump(isExec('1122242') );
//运行结果 Array [ [0] => 11*22=242 ] bool(true)
var_dump(isExec('11234'));
//运行结果 bool(false)
Related recommendations:
Use PHP code to verify a string Is the number connected into a string of numbers?
The above is the detailed content of How to determine whether a string of numbers is multiplied consecutively in PHP. For more information, please follow other related articles on the PHP Chinese website!

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools
