note 109139 modified in function.explode by danbrown

From: Date: Tue, 26 Jun 2012 13:56:53 +0000
Subject: note 109139 modified in function.explode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188830@lists.php.net to get a copy of this message
<?php
function blowup($delimiter,$string){
		if(strpos($string,$delimiter)===false){
			return array($string);
		}
		else{
			return explode($delimiter, $string);
		}
	}
?>

returns you an array always. if it doesn't find the delimiter it simply returns the string but as an array.

example:

<?php echo blowup('@','cat@dog@mouse'); ?>

returns:

Array('cat','dog','mouse');

example:

<?php echo blowup('@','cat'); ?>
returns:
Array('hello');

Useful if you have to have at least ONE option

[EDIT BY danbrown AT php DOT net: DISCLAIMER - As you may have determined by now, the author meant that the second example will return Array('cat'). The function does not teach felines how to greet the user in the English language.] --was-- function blowup($delimiter,$string){ if(strpos($string,$delimiter)===false){ return array($string); } else{ return explode($delimiter, $string); } } returns you an array always. if it doesn't find the delimiter it simply returns the string but as an array. example: echo blowup('@','cat@dog@mouse'); returns: Array('cat','dog','mouse'); example: echo blowup('@','cat'); returns: Array('hello'); Useful if you have to have at least ONE option http://php.net/manual/en/function.explode.php

« previous php.notes (#188830) next »