0

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

2
  • How is the script supposed to know that Engineering Maths-I should be kept together? What's the rule? Commented Jul 19, 2013 at 21:39
  • @Barmar Think it's probably tab separated and browser is collapsing the space. Commented Jul 19, 2013 at 21:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

THankyou so much for ur hint sir, actually it is \n nd I will accept ur answer in 9 mins, please edit ur answer
1

You can also try to split on whitespace:

$n = preg_split('/\s+/', $string);

1 Comment

That will split the Engineering and Maths-I... which is not what OP wants

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.