-2

I have some strings in loop like below example.

1) Sold by Olympia Industries Ltd. (4.7 out of 5 | 33,445 ratings) and Fulfilled by Amazon. Gift-wrap available.

2) Sold by Health first trading (4.3 out of 5 | 47 ratings) and Fulfilled by Amazon. Gift-wrap available.

3) Sold and fulfilled by hamsaveni (4.1 out of 5 | 101 ratings).

4) Sold and fulfilled by Coffee Shopee (3.4 out of 5 | 7 ratings).

What i want is to extract the name of seller i.e.

1) Olympia Industries Ltd. 2) Health first trading 3) hamsaveni 4) Coffee Shopee

I know we can do it by matching the first brace "(" and with some if else condition. But unfortunately i am not able to do it.

Any help will really appreciated.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Sharique Anwer
  • 129
  • 2
  • 13

3 Answers3

1

How about:

preg_match('/by([^(]+)/', $string, $matches);
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Thanks Toto and also Murat. It worked! preg_match('/by([^(]+)/', $seller2, $matches); $seller=str_replace("by ","",$matches[0]); – Sharique Anwer Jan 18 '17 at 13:02
0

I consider that seller name is start after "by" and end before the (. then

it will work for you.

substr($string,strpos($string,'by')+2,strpos($string,'(')-strpos($string,'by')-2);

0

I hope people don't mind me using .NET regex syntax (it's pretty similar), but here's what I came up with:

(?:by)([A-Za-z ]+)(?:\([0-9]+(\.[0-9]+)?)

Basically, the name is a captured group consisting of some combination of letters and spaces that occurs after the word "by" (which is a non-capturing group, since all you actually care about capturing is the name) and a rating (also a non-capturing group).

Community
  • 1
  • 1