Page 1 of 1
Breaking up a string
Posted: Sat Feb 28, 2004 11:35 am
by digital5
I have a string e.g.
msg style this is a test message
I would like to break this up in to three variables. The first part 'msg' will always be the same. the second part 'style' will always vary in content and length but will always be one single word (the break point will be the space) and then the third part 'this is a message' will always be different and will vary in length and content.
I have been looking at the substr() function and think this might work for me but not to sure, just wondered if anyone could point me in the right direction.
Sy
Posted: Sat Feb 28, 2004 11:53 am
by Weirdan
I think [php_man]preg_match[/php_man] will suit better:
Code: Select all
$string = "msg style this is a test message";
$pattern="/(msg) (\S*) (.*)/i";
preg_match($pattern,$string,$matches);
echo $matches[1];// "msg"
echo $matches[2];// "style"
echo $matches[3];// "this is a test message"
Posted: Sat Feb 28, 2004 11:59 am
by Illusionist
You could use str_replace() to remove the first msg which will always be the same everytime, then user substr() and strpos() to get the text fromt he beginning to the enxt space, whcih would be the style or w/e, then take that part out, and then the rest is your actual message!
Posted: Sat Feb 28, 2004 12:10 pm
by digital5
I used this and it worked like a treat for me. Thanks
Weirdan wrote:I think [php_man]preg_match[/php_man] will suit better:
Code: Select all
$string = "msg style this is a test message";
$pattern="/(msg) (\S*) (.*)/i";
preg_match($pattern,$string,$matches);
echo $matches[1];// "msg"
echo $matches[2];// "style"
echo $matches[3];// "this is a test message"
Posted: Sat Feb 28, 2004 12:42 pm
by Illusionist
ah, i didn't even think of that!
Posted: Sat Feb 28, 2004 12:57 pm
by EvilWalrus
Don't use preg unless its necessary.. .it'll slow your script down a bit where it doesn't need to be slowed down.
To split by individual chars....
Code: Select all
<?php
$string = 'this is a test message';
$array = array();
for ($x=0; $x<strlen($string); $x++) {
$array[] = $string{$x};
}
?>
To split by space, just explode(' ', $string);
Simple.
Posted: Sat Feb 28, 2004 1:19 pm
by Weirdan
It's the usual tradeoff between speed and code clarity.
My own rules of optimization:
- Do not optimize unless it's necessary. Write clean code instead.
- Write first, then test. And then optimize. Or not.
- Start optimization from bottlenecks. You don't need to optimize every bit of your program.
- My time costs more than CPU time.
It wasn't the question on how to split by space, by the way

Posted: Sat Feb 28, 2004 2:07 pm
by digital5
the way Weirdan explained works for me and it's only a small script in this instance so it won't make much difference, thanks anyway for all your answers.
Posted: Sun Feb 29, 2004 3:03 am
by digital5
even though this worked from what i asked, i have noticed it does not work if the third section is empty e.g. the string is 'msg style'. Basically i'm running an if statement on the second part 'style' and if the third part is empty the if statement fails.
What would i need to change to make it work if the third section is empty
Sy
Weirdan wrote:I think [php_man]preg_match[/php_man] will suit better:
Code: Select all
$string = "msg style this is a test message";
$pattern="/(msg) (\S*) (.*)/i";
preg_match($pattern,$string,$matches);
echo $matches[1];// "msg"
echo $matches[2];// "style"
echo $matches[3];// "this is a test message"
Posted: Sun Feb 29, 2004 9:08 am
by markl999
I'd just keep it simple, Eg :
Code: Select all
<?php
$string = 'msg style this is a test message'; //or just 'msg style'
$parts = explode(' ', $string);
$first = $partsї0];
$second = $partsї1];
$third = join(' ', array_slice($parts, 2));
echo $first.'*'.$second.'*'.$third; //test line
?>
Posted: Sun Feb 29, 2004 10:30 am
by digital5
looks good to me markl999, worked like a charm.
Posted: Mon Mar 01, 2004 1:05 pm
by Weirdan
Isn't it simple:
Code: Select all
$string = "msg style this is a test message";
$pattern="/(msg) (\S*) (.*)?/i";
preg_match($pattern,$string,$matches);
echo $matches[1];// "msg"
echo $matches[2];// "style"
if(isset($matches[3]))echo $matches[3];// "this is a test message"
else echo "third part is empty";