0

I have over one thousand reviews in a string. I have been asked to see if i can get the average rate from each author. an example of the string is below. I have highlighted the content which I need to extract into an array.

Shepherd's Bush Empire (Times p54 - 4* David Sinclair)Grizzly Bear - Serpentine Sessions, Hyde Park, London (Independent Viewspaper p17 - 4* Elisa Bray)Blondie – Kenwood House (Sun p69 – 4* JJ)Jeff Tweedy - Union Chapel, London (Independent Viewspaper p17 - 4* Enjoli Liston)Tony Bennett - Albert Hall, London (Times p68 - 4* Clive Davis, Standard p40 – 5* Jack Massarick

Is there anyway to explode the string on * go back one character and forward to words.

I know there will be odds but these can be removed later. is there a way of using preg_split or explode?

Machavity
  • 30,841
  • 27
  • 92
  • 100
gareth
  • 179
  • 2
  • 18
  • I expect http://php.net/manual/en/function.preg-match-all.php would be the base of your solution, but my regex foo is sadly lacking – Steve Nov 04 '14 at 16:59

2 Answers2

0

You could use a regex something like:

([0-9]+) *\* *([^\)]+) 

Should do the trick.

This looks for 1 or more digits, (saving them), followed by zero or more discarded spaces, a literal *, followed by any number of discarded spaces, followed by any number of any characters but literal ), saving them.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
0

find a single number followed by an asterisk, then two words separated by a space

preg_match_all("/\d\* ?\w* ?\w*/", $input_lines, $output_array);

and here's an example link http://www.phpliveregex.com/p/7Ty

your output array will look like this

Array
(
    [0] => Array
        (
            [0] => 4* David Sinclair
            [1] => 4* Elisa Bray
            [2] => 4* JJ
            [3] => 4* Enjoli Liston
            [4] => 4* Clive Davis
            [5] => 5* Jack Massarick
        )

)

EDIT:: for adding an optional decimal place use this /\d(\.\d{1,2})?\* ?\w* ?\w*/

Zombiesplat
  • 943
  • 8
  • 19