PHP

Splitting String


 

The preg_split() function in PHP is used to split a string into an array using a regular expression pattern as the delimiter. It is a more flexible alternative to the explode() function when you need to use complex patterns for splitting.

 

Syntax:

<?php
		preg_split(pattern, subject, limit, flags);
	?>

 

Example:

<?php
		$string = "This is a normal string";
		$pattern = "/\s+/"; // Split by one or more spaces
		$array = preg_split($pattern, $string);
		print_r($array);
	?>

 

Output: