PHP preg_split to split a string with specific values?


For this, use preg_match_all(). Let’s say the following is our string 

$values = 'javamysqlphpmongodbpythonspringhibernatephp';

We want to split with specific values like 

java
hibernate
php

Example

The PHP code is as follows 

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
$values = 'javamysqlphpmongodbpythonspringhibernatephp';
$afterSpliting = preg_match_all("/(java|hibernate|php)/", $values, $result);
print_r($result);
?>
</body>
</html>

Output

This will produce the following output

Array ( [0] => Array ( [0] => java [1] => php [2] => hibernate [3] => php ) [1] => Array ( [0] => java [1] => php [2] => hibernate [3] => php ) )

Updated on: 13-Oct-2020

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements