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
https://forums.phpfreaks.com/topic/146972-extract-value-between-2-matches/
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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.