Page 1 of 1
How do I return part of a string?
Posted: Thu Jan 18, 2007 5:21 pm
by impulse()
I can use
to return the part after the needle if found. But I want a function that returns everything before the needle.
EG:
Code: Select all
$string = "autoexec.bat";
echo whatFunction(".", $string);
# outputs autoexec.
Posted: Thu Jan 18, 2007 5:42 pm
by Kieran Huggins
explode()
preg_split()
split()
all come to mind
Posted: Thu Jan 18, 2007 6:02 pm
by aaronhall
Don't use
preg_split() or
split() unless you need to use regular expressions to find the needle;
explode() will do.
You could also use
strpos() and
substr() together
Posted: Thu Jan 18, 2007 6:22 pm
by Ollie Saunders
preg_split and split and explode? What are you guys thinking
Code: Select all
substr($haystack, 0, strpos($haystack, $needle));
Posted: Thu Jan 18, 2007 6:26 pm
by aaronhall
ole wrote:preg_split and split and explode? What are you guys thinking
Code: Select all
substr($haystack, 0, strpos($haystack, $needle));
Did you read my post?
Posted: Thu Jan 18, 2007 6:27 pm
by Ollie Saunders
Oh yeah sorry Aaron.