note 107640 modified in function.explode by danbrown

From: Date: Thu, 10 May 2012 20:26:26 +0000
Subject: note 107640 modified in function.explode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187806@lists.php.net to get a copy of this message
If you don't want any empty array element, just use simply "preg_split" instead this function (or struggle with an extra function, i.e. "trim");

<?php
$uri = "/user/123/";

$tmp = explode("/", $uri);
print_r($tmp);

$tmp = explode("/", trim($uri, "/"));
print_r($tmp);

$tmp = preg_split("~/~", $uri, -1, PREG_SPLIT_NO_EMPTY);
print_r($tmp);
?>

Array
(
 [0] => 
 [1] => user
 [2] => 123
 [3] => 
)
Array
(
 [0] => user
 [1] => 123
)
Array
(
 [0] => user
 [1] => 123
) --was-- If you don't want any empty array element, just use simply "preg_split" instead this function (or struggle with an extra function, i.e. "trim"); $uri = "/user/123/"; $tmp = explode("/", $uri); print_r($tmp); $tmp = explode("/", trim($uri, "/")); print_r($tmp); $tmp = preg_split("~/~", $uri, -1, PREG_SPLIT_NO_EMPTY); print_r($tmp); Array ( [0] => [1] => user [2] => 123 [3] => ) Array ( [0] => user [1] => 123 ) Array ( [0] => user [1] => 123 ) http://php.net/manual/en/function.explode.php

« previous php.notes (#187806) next »