By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Explode

The1corrupted
100+
P: 134
I have a question about the explode function and am rather confused...
Say $msg=I like Corn
If I have [PHP]<?php
$mesg=explode(" ", $msg, 3);
echo $mesg[0].$mesg[2];
?>[/PHP]
will I manage to be able to pull this result:
"I like"?
Feb 27 '07 #1
Share this Question
Share on Google+
3 Replies


ronverdonk
Expert 2.5K+
P: 4,258
What you are echoeing in your sample is "ICorn" because you used the first (msg[0]) and the third (msg[2]) entries without connecting blank character.

In order to display "I like" your do[php]echo $mesg[0].' '.$mesg[1];[/php]

Ronald :cool:
Feb 28 '07 #2

The1corrupted
100+
P: 134
Ah... Also, can I double explode strings? Say...

[PHP]
<?php
$mesg=explode(" ", $msg, 2);

if ($mesg[0]=="word") {
$mesg=explode(" ", $msg, 3);
}

?>[/PHP]
Or would I have to redefine $mesg in the if statement as another variable?
Mar 1 '07 #3

10K+
P: 13,264
Ah... Also, can I double explode strings? Say...

[PHP]
<?php
$mesg=explode(" ", $msg, 2);

if ($mesg[0]=="word") {
$mesg=explode(" ", $msg, 3);
}

?>[/PHP]
Or would I have to redefine $mesg in the if statement as another variable?
Use another variable for the array

Expand|Select|Wrap|Line Numbers
  1.  <?php 
  2. $mesgArray1=explode(" ", $msg, 2);
  3.  
  4. if ($mesgArray1[0]=="word") {
  5. $mesgArray2=explode(" ", $msg, 3);
  6. }
  7. ?>
  8.  
The value of $msg remains the same in this case
Mar 2 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.