Re: suffixSwap function
| From: | Matt McClanahan | Date: | Fri, 06 Oct 2000 04:57:48 +0000 |
| Subject: | Re: suffixSwap function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18835@lists.php.net to get a copy of this message | ||
On Thu, 5 Oct 2000, Theodore Jones wrote:
> Hey all,
>
> I've go this function:
>
> function suffixSwap($nameString, $x) {
> $pattern = ".";
> $arr = split($pattern, $nameString);
> echo ($arr[0]) . $x;
> }
Since you're doing a very basic seperation, using a regex is overkill.
It'd be less work for the engine to use explode(), which is intended for
breaking up a string based on one character.
http://www.php.net/manual/function.explode.php
The reason the split() wasn't behaving as you expected was that split was
receiving only a . as its pattern, not a '.' as you wanted it to. To
split() using a literal '.' as a pattern, you would've needed to say
$pattern = "'.'" or something along those lines.
> Anyone know what could be going on. It just spits out ".mpg" for every
> value I try with my function call:
>
> suffixSwap("stardust.jpg", ".mpg")
Try this version, using explode instead:
function suffixSwap($nameString, $x)
{
$pattern = '.';
$arr = explode($pattern,$nameString);
echo $arr[0] . $x;
}
A fair warning though, this won't work for files that have multiple
periods in them.
Matt