How to extract data (string) using php?

I am new to php and I don't know how to extract the exact things I want in a string. eg:

Instant Oats Drink - Chocolate Flavour 165g (33g x 5) 

I want to extract this data so that

 Name: Instant Oats Drink - Chocolate Flavour Weight: 165g number of pack: 5 unit weight: 33g 

Please help me with this question.

+4
source share
4 answers

If all your lines have the same form, you can use regular expressions.

think that:

 <?php $str = "Instant Oats Drink - Chocolate Flavour 165g (33g x 5)"; preg_match('/(?P<title>[a-zA-Z\s\-]*)(?P<grammars>[0-9]*g)(\s+\()(?P<portion>[0-9]+g)(\s+x\s+)(?P<times>[0-9]+)(\))/', $str, $m); echo "Title : " . $m['title'] . '<br />'; echo "Grammars : " . $m['grammars'] . '<br />'; echo "Portion : " . $m['portion'] . '<br />'; echo "Times : " . $m['times'] . '<br />'; ?> 

Based on @Troy answer you can even use one that is even sorted

 <?php $str = "Instant Oats Drink - Chocolate Flavour 165g (33g x 5)"; preg_match('/(?P<title>.*) (?P<grammars>\d+g) \((?P<portion>\d+g) x (?P<times>\d+)\)/', $str, $m); echo "Title : " . $m['title'] . '<br />'; echo "Grammars : " . $m['grammars'] . '<br />'; echo "Portion : " . $m['portion'] . '<br />'; echo "Times : " . $m['times'] . '<br />'; ?> 
+1
source

To extract data from strings in php, you can use functions like preg_match, explode, ereg

You will need to clarify the exact format in which all rows will work correctly, because you need to have the specific data format that you are looking for with your functions.

0
source

If the lines change as much as you mean, then it will be practically impossible to do exactly. And it is in any language; nothing about PHP will become simpler or harder: it is a problem with natural language analysis, and it is very difficult to automate.

Regex (using the preg_split() function) can do the job if the lines are regular, but not for random lines like these. Each of them is its own special case, so you need to write a separate bit of code for each of them ... but in this case, you could just do the splitting manually.

The only way to get this kind of data without manual intervention is if the data provider (i.e. the site that you linked in the comments?) Can provide its preliminary formatting. Perhaps they already have data divided into the required format in their own product database, so if they are ready to provide it, this will work for you.

They would almost certainly want to charge for the data, but given the amount of effort involved in doing this manually, it was probably worth it.

0
source

I'm not sure how specific the answer you are looking for is, but here is a regular expression that parses your string. It will work for any line that closely follows the same pattern.

 <?php $s = "Instant Oats Drink - Chocolate Flavour 165g (33g x 5)"; preg_match("/(.*) (\d+g) \((\d+g) x (\d+)\)/", $s, $matches); var_dump($matches); ?> 

Outputs:

 array(5) { [0]=> string(53) "Instant Oats Drink - Chocolate Flavour 165g (33g x 5)" [1]=> string(38) "Instant Oats Drink - Chocolate Flavour" [2]=> string(4) "165g" [3]=> string(3) "33g" [4]=> string(1) "5" } 
0
source

All Articles