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?
split string
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
Mac
Code: Select all
$string = "'first part' | 'second part' | 'third part'";
$parts = explode(' | ', $string);- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
You could try it:output:
Regards,
Scorphus.
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";
?>Code: Select all
part1
part2
part3 that contains white spaces as wellScorphus.