Breaking up a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
digital5
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2004 6:07 am
Location: Chelmsford, UK

Breaking up a string

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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"
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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!
digital5
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2004 6:07 am
Location: Chelmsford, UK

Post 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"
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

ah, i didn't even think of that!
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 ;)
digital5
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2004 6:07 am
Location: Chelmsford, UK

Post 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.
digital5
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2004 6:07 am
Location: Chelmsford, UK

Post 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"
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I'd just keep it simple, Eg :

Code: Select all

&lt;?php
$string = 'msg style this is a test message'; //or just 'msg style'
$parts = explode(' ', $string);
$first = $parts&#1111;0];
$second = $parts&#1111;1];
$third = join(' ', array_slice($parts, 2));
echo $first.'*'.$second.'*'.$third; //test line
?&gt;
digital5
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2004 6:07 am
Location: Chelmsford, UK

Post by digital5 »

looks good to me markl999, worked like a charm.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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";
Post Reply