- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 )
- Related Articles
- How do I verify that a string only contains letters, numbers, underscores and dashes in Python?
- PHP fetch a specific value that begins with a given number from a string with letters and numbers?
- How do I split a multi-line string into multiple lines?
- How do I split a string, breaking at a particular character in JavaScript?
- PHP preg_split to split a string with specific values?
- How do I split a numerical query result in MySQL?
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- How do I use arrays in C++?
- How do I check if a string has alphabets or numbers in Python?
- How do we use a delimiter to split string in Python regular expression?
- How to split comma and semicolon separated string into a two-dimensional array in JavaScript ?
- How do I remove a substring from the end of a string in Python?
- The easiest way to concatenate two arrays in PHP?
- How do I create a Java string from the contents of a file?
- How to split a string in Python

Advertisements