Breaking up a string
Moderator: General Moderators
Breaking up a string
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
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
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"-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
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"
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
- EvilWalrus
- Site Admin
- Posts: 209
- Joined: Thu Apr 18, 2002 3:21 pm
- Location: Springmont, PA USA
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....
To split by space, just explode(' ', $string);
Simple.
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};
}
?>Simple.
It's the usual tradeoff between speed and code clarity.
My own rules of optimization:
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.
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
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"
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
?>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";