![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I split the letters and numbers to two arrays from a string in PHP
For this, use preg_split() along with array_shift() and array_pop(). Let’s say the following is our string with letters and numbers
$values = "5j4o5h8n";
We want to display the numbers and letters in separate arrays.
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $values = "5j4o5h8n"; $singleValues = preg_split("/\d+/", $values); array_shift($singleValues); print_r($singleValues); $resultNumber= preg_split("/[a-z]+/", $values); array_pop($resultNumber); print_r($resultNumber); ?> </body> </html>
Output
This will produce the following output
Array ( [0] => j [1] => o [2] => h [3] => n ) Array ( [0] => 5 [1] => 4 [2] => 5 [3] => 8 )
Advertisements