note 100729 modified in function.preg-split by danbrown

From: Date: Wed, 03 Nov 2010 13:27:01 +0000
Subject: note 100729 modified in function.preg-split by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173709@lists.php.net to get a copy of this message
If you want to split by a char, but want to ignore that char in case it is escaped, use a lookbehind assertion. In this example a string will be split by ":" but "\:" will be ignored: <?php $string='a:b:c\:d'; $array=preg_split('#(?<!\\\)\:#',$string); print_r($array); ?> Results into: Array ( [0] => a [1] => b [2] => c\:d ) --was-- If you want to split by a char, but want to ignore that char in case it is escaped, use a lookbehind assertion. In this example a string will be split by ":" but "\:" will be ignored: $string='a:b:c\:d'; $array=preg_split('#(?<!\\\)\:#',$string); print_r($array); Results into: Array ( [0] => a [1] => b [2] => c\:d ) http://php.net/manual/en/function.preg-split.php

« previous php.notes (#173709) next »