-1

Using the following code that I already have:

$var =  "City 0.428371 2 BEIJING Beijing 39.91388888888889 116.39166666666667  Organization 0.392154 1 PBOC PBOC Motorsports Club  Company 0.38134 1";

Need to extract this:

City 0.428371 2 BEIJING

  • 2
    What have *you* tried so far? -- (SO isn't a code writing service). See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Apr 13 '13 at 14:58
  • I have made this \bCi(\w*)y\b [0-9.]+ [0-9]+ [a-zA-Z]+ bt I got error – Mohammed Ahsan Apr 13 '13 at 15:27
  • **Try writing something yourself** and then if it doesn't work, bring it to us to help you along. You start it, we help. We don't write it for you. Show us the actual code that you've tried and then we can help you from there. Chances are you'll get pretty close to the answer if you just try it yourself first. – Andy Lester Apr 13 '13 at 15:35
  • Before you can write a regular expression, you have to be able to describe, in English, the rules that you're trying to implement. – Andy Lester Apr 13 '13 at 15:35

2 Answers2

1

What did you try?

This would seem to do the trick:

$parts = explode(' ', $var, 5);
array_pop($parts);
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Evert
  • 93,428
  • 18
  • 118
  • 189
0
$extracted = implode(' ', array_slice(explode(' ', $var), 0, 4));

Bejing has to be one word, wouldn't work with "New York" for example.

enrey
  • 1,621
  • 1
  • 15
  • 29