1

I have a text file in which each line is an entry. each line has certain words separated by space and 2 -3 words at the end of the entry with brackets

eg.

asd asdasd asdasd {m} [jsbbsdfb] 

how do I get the following result in php

$data[0]="asd asdasd asdasd"

$data[1]= "{m}"

$data[2]= "[jsbbsdfb]"
1
  • What have you tried? theres a couple of ways of splitting strings, with split, reg expressions etc. Show us what you tried, we can help you more. Commented Jun 4, 2011 at 10:17

5 Answers 5

1

You can use substring and strpos to find the right parts or you can use preg_match (regular expressions), both have their own (dis)advantages. Using preg_match you can do this is as little as one line, but it is relatively slow and not as easy to use. I would do something like this:

while(/* $line is next line available */) {
  $brace_position = strpos($line, '{');
  $bracket_position = strpos($line, '[');

  $data = array(
    substr($line, 0, ($brace_position - 1),
    substr($line, $brace_position, ($backet_position - $brace_position - 1)),
    substr($line, $bracket_position)
  );
}

The other answer provided above, by wired00, are correct too, but only if each line is exactly the same in length (i.e. the same amount of words an spaces). The answer by check123 is not a correct answer to your question.

See strauberry's answer for the regular expression which I mentioned in above.

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

Comments

1
$regex = '/([a-zA-Z ]*) (\{[a-zA-Z]\}) (\[[a-zA-Z]+\])/';
$yourText = "asd asdasd asdasd {m} [jsbbsdfb]";
$lines = explode("\n", $yourText);
foreach($lines as $line) {
    $result = array();
    preg_match($line, $regex, $result);
    print_r($result);
}

You can tune the regex of course :)

Comments

0

few ways... you could try exploding on the spaces... then concatenate the first 3 array elemnents together

something like this:

$yourString = "asd asdasd asdasd {m} [jsbbsdfb]";
$elements = explode ( " " , $yourString);

$string1 = $elements[0] . $elements[1] . $elements[2];
$string2 = $elements[3];
$string3 = $elements[4];

you could obviously then just set the values into first 3 elements of the array if you wanted

ie:

$yourString = "asd asdasd asdasd {m} [jsbbsdfb]";
$elements = explode ( " " , $yourString);

$elements[0] = $elements[0] . $elements[1] . $elements[2];
$elements[1] = $elements[3];
$elements[2] = $elements[4];

Comments

0
$x = "asd asdasd asdasd {m} [jsbbsdfb]";

$a1 = strpos($x, '{');
$a2 = strpos($x, '}', $a1);
$b1 = strpos($x, '[', $a2);
$b2 = strpos($x, ']', $b1);

$data = array();
$data[] = substr($x, 0, $a1-1);
$data[] = substr($x, $a1+1, $a2-$a1-1);
$data[] = substr($x, $b1+1, $b2-$b1-1);

var_dump($data);

Comments

0

$a = explode(" ",$string); $a[1] //will store first word of your string $a[2] // and so on until the end of the string

Ok! I got it wrong above. You might want to try something like:

$str = "asd asdasd asdasd {m} [jsbbsdfb]";
$k=strlen($str);
for($i=0;$i<$k;$i++) {
    $char = substr($str,$i,1);
    //do your operation here on
}

and loop through each character of the string and operate as required.

6 Comments

That would split his string into 5 parts.. He'd do better to do it by { and then by [ for the second half.
This is not the proper answer to this @check123
But it seems his brackets and words do not have space in between them, so explode may be helpful.
see his example output and think about the output that your answer will produce. DO they match? I guess not.
@coding-Freak: ok! got the difference!
|

Your Answer

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