Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

A php variable contains

$string = "256 Engineering Maths-I 21 -1 21 F";

printing

$string

gives output

256 Engineering Maths-I 21 -1 21 F

this variable should split in to

$n[0] = "256";
$n[1] = "Engineering Maths-I";
$n[2] = "21";
$n[3] = "-1";
$n[4] = "21";
$n[5] = "F";

I have tried with

$n = explode(" ", $string);

but it is splitting in to 2 parts

Please help me

share|improve this question
    
How is the script supposed to know that Engineering Maths-I should be kept together? What's the rule? –  Barmar Jul 19 '13 at 21:39
    
@Barmar Think it's probably tab separated and browser is collapsing the space. –  Orangepill Jul 19 '13 at 21:40
add comment

2 Answers

up vote 3 down vote accepted

What you are probably looking at is a tab separated string

Do this

$n = explode("\t", $string);

UPDATE The answer was that the text was delimited by a newline. so

$n = explode("\n", $string); 

The browser's behavior of collapsing whitespace to a single space was masking what was really happening.

share|improve this answer
    
THankyou so much for ur hint sir, actually it is \n nd I will accept ur answer in 9 mins, please edit ur answer –  Raj Jul 19 '13 at 21:43
add comment

You can also try to split on whitespace:

$n = preg_split('/\s+/', $string);
share|improve this answer
1  
That will split the Engineering and Maths-I... which is not what OP wants –  Orangepill Jul 19 '13 at 21:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.