Jump to content

extract value between 2 matches


drisate

Recommended Posts

Hey guys i would like to find and loop a string and extract the value in between the markes into an array ... ex:

 

 

1221344136613

 

between 1 and 13

 

array[0] = 22

array[1] = 44

array[2] = 66

 

i created this

 

function findinside($start, $end, $string)

{

    preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)' . preg_quote($end,

        '/') . '/i', $string, $m);

    return $m;

}

 

But i dont think it's returning an array because i am looping the result into a foreach for then extract more values in the string. and i get

 

preg_match_all() expects parameter 2 to be string, array given

 

this is my full code:

$content = file_get_contents("$page");

function findinside($start, $end, $string)
{
    preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)' . preg_quote($end,
        '/') . '/i', $string, $m);
    return $m;
}

$start = '<table cellpadding="0" cellspacing="0" border="0" style="width:100%"><tr><td width="100%"><table cellpadding="4" cellspacing="1" border="0" style="width:100%" class="tableinborder">';
$end = "</table></td></tr></table>";


$out = findinside($start, $end, $content);

foreach ($out as $loop)
{

    ////////////////
    // USERNAME
    $txt = $loop;

    $re = '.*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?((?:[a-z][a-z0-9_]*))';# Non-greedy match on filler

    if ($c = preg_match_all("/".$re."/is", $txt, $matches))
    {
        $var1 = $matches[1][0];
        print "($var1) \n";
    }


    ///////////////
    // TIME

    $re = '((?:[0]?[1-9]|[1][012])[-:\\/.](??:[0-2]?\\d{1})|(?:[3][0,1]{1}))[-:\\/.](??:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d]).*?((??:[0-1][0-9])|(?:[2][0-3])|(?:[0-9]))?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)';# MMDDYYYY 1

    if ($c = preg_match_all("/".$re."/is", $txt, $matches))
    {
        $mmddyyyy1 = $matches[1][0];
        $time1 = $matches[2][0];
        print "($mmddyyyy1) ($time1) \n";
    }

}

 

I am basacly looping the content of a board thread and extract the username a time the post was posted.

Link to comment
Share on other sites

umm no hehe i wana loop a board thread then extract for every messages of the page the username that posted the message and the time it was posted.

 

What i need to do is breake the page in to a loop for every threads and load that to an array to then loop that array and extract the needed info.

 

Where i block is extracting the threads between 2 values

Link to comment
Share on other sites

nah it's not abbout the string it self... that was just an exemple that fits in one line hehe. Like i said i am looping the HTML page of a board. it needs to loop every message posted and extract the values...

 

$start = '<table cellpadding="0" cellspacing="0" border="0" style="width:100%"><tr><td width="100%"><table cellpadding="4" cellspacing="1" border="0" style="width:100%" class="tableinborder">';

 

$end = "</table></td></tr></table>";

 

Is how i can seperate the page. what i need to do is loop every result of whats between

Link to comment
Share on other sites

I'm not that great at regular expressions, so I'm not sure if this would work in 100% of cases, but it works with the example you provided:

 

$string = "1221344136613";

preg_match_all ( '/1{0,1}(.*?)(13)/', $string, $matches );

print_r($matches[1]);

 

Output

    [0] => 22
    [1] => 44
    [2] => 66

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.