Jump to content

Extracting data with regex


ItsPawl

Recommended Posts

Hello,

 

Im pretty new to PHP and completely new to Regex, so bare with me.

 

Im trying to extract data in from a html and save in my own variables.

For example from something like:

<fruit="Carrots">Carrots<class="fruit">4,3<class="fruit">13,00

i would like to save the fruit name and corresponding price which is the second number (13,00 in this case). I would then like to find the same for all listed fruits.

 

I was thinking that it might be possible to extract those strings by using regex to filter out the information i want, but i cant figure out how. The problem for me is to extract the price which i know to be the second number, but i cant find a unique string that preceeds it and the start index of the number may also vary.

 

Appreciate any help.

 

*Edit

Sry, i just realized that there where a child forum for regex questions. Hope to get help anyway if its no problem.

Link to comment
Share on other sites

Well, its not quite like that. I just wrote what came to mind. The pattern is the same as in the html though, so if there is any solution you can think of using regex then let me know.

 

Trying to read up on regex, but seem like there is a lot to learn.

Link to comment
Share on other sites

try

<?php
$test = '<fruit="Carrots">Carrots<class="fruit">4,3<class="fruit">13,00';
preg_match_all('/<fruit="([^"]*)"\D+>[0-9,]+\D+>([0-9,]+)/', $test, $out);
$out = array_combine($out[1], $out[2]);
print_r($out);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.