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

 Live Demo

<!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 )

Updated on: 13-Oct-2020

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements