Page 1 of 1

split string

Posted: Tue Oct 28, 2003 10:04 am
by yaron
Hello all,

I want to split a string to 3 substrings.
my string format is :

'first part' 'second part' 'third part'

whitespace is deviding each part but the third part conatin whitspaces as well so I can't use explode(' ',$str) becasue it will devide the third part to many subs as well.

and thoughts?

Posted: Tue Oct 28, 2003 10:13 am
by twigletmac
Is there no way you could add another delimiter? It would be possible to split the string as is (if those single quotes are part of it) but it would be much easier if it were, for example pipe (|) delimited:

Code: Select all

$string = "'first part' | 'second part' | 'third part'";
$parts = explode(' | ', $string);
Mac

Posted: Tue Oct 28, 2003 10:20 am
by yaron
if I knew where to put the delimiter '|' then I wouldn't have any problem.
the string is coming out of a file.
What I need is to explode only the first 2 ones devided by whitespace and the third one is on it's own and can contain whitespaces

Posted: Tue Oct 28, 2003 10:33 am
by scorphus
You could try it:

Code: Select all

<?php
$str = 'part1 part2 part3 that contains white spaces as well';
$parts = explode(' ', $str);
$first = $parts[0];
$secon = $parts[1];
$third = $parts[2];
for ($i = 3; $i < count($parts); $i++)
	$third .= ' ' . $parts[$i];

echo $first, "\n";
echo $secon, "\n";
echo $third, "\n";
?>
output:

Code: Select all

part1
part2
part3 that contains white spaces as well
Regards,
Scorphus.

Posted: Tue Oct 28, 2003 11:04 am
by yaron
thanks.. i'll do that