
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP preg_split with Hyphen
Use preg_split() in PHP and split with hyphen. Let’s say the following is our input value with numbers and string separated with hyphen
$values ="ABC-DEF IJKL-3553435-8990987876";
We want the output to be
Array ( [0] => ABC-DEF IJKL [1] => 3553435 [2] => 8990987876 )
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $values ="ABC-DEF IJKL-3553435-8990987876"; $exploded = preg_split('/-(?=[0-9])/', $values, 3); print_r($exploded); ?> </body> </html>
Output
This will produce the following output
Array ( [0] => ABC-DEF IJKL [1] => 3553435 [2] => 8990987876 )
Advertisements