- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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>
This will produce the following output
Array (  => j  => o  => h  => n ) Array (  => 5  => 4  => 5  => 8 )
- 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 numerical query result in MySQL?
- How do I use arrays in C++?
- How do I create a Java string from the contents of a file?
- How do I remove a substring from the end of a string in Python?
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- The easiest way to concatenate two arrays in PHP?
- How do I check if a string has alphabets or numbers in Python?
- PHP preg_split to split a string with specific values?
- MongoDB query to retrieve records from a collection named with letters and numbers
- How do I remove a string from an array in a MongoDB document?
- How can I concatenate two arrays in java