forum.coppermine-gallery.net

Support => cpg1.4.x Support => Older/other versions => cpg1.4 plugins => Topic started by: Timos-Welt on June 20, 2008, 12:40:23 pm

Title: [Solved]: preg_match to get URL
Post by: Timos-Welt on June 20, 2008, 12:40:23 pm
I'm trying to get $album, $cat and $pos out of each thumb URL from album list and thumbnails pages for standard mode and SEF mode.

So I filter the page $html and do a preg_match_all (simplyfied source code):

Code: [Select]
  // get search string depending on SEF or not
  if (!$ENLARGEITSET['enl_sefmode']) $ausdruck = "#...href=\"displayimage.php\?(.*)\">...#i";
  else $ausdruck = "#...href=\"displayimage-(.*)\.html\">...";
 
  // get matches
  preg_match_all($ausdruck, $html, $treffer, PREG_SET_ORDER);

So now I have the matches in $treffer.

Now I iterate over them and try to match the three or two variables out of match[1]:

Code: [Select]
  foreach($treffer as $match) {

        if (!$ENLARGEITSET['enl_sefmode']) preg_match_all("/album=(.+)cat=(.+)pos=(.+)/",$match[1],$enl_gotit);
        else preg_match_all("/(.+)-(.+)-(.+)/",$match[1],$enl_gotit);
 
        if ($enl_gotit[0]) {
          $album = (!$ENLARGEITSET['enl_sefmode']) ? rtrim($enl_gotit[1][0],"&") : $enl_gotit[1][0];
          $cat = (!$ENLARGEITSET['enl_sefmode']) ? rtrim($enl_gotit[2][0],"&") : $enl_gotit[2][0];
          $pos = (!$ENLARGEITSET['enl_sefmode']) ? rtrim($enl_gotit[3][0],"&") : $enl_gotit[3][0];
        }
        else
        {
          if (!$ENLARGEITSET['enl_sefmode']) preg_match_all("/album=(.+)pos=(.+)/",$match[1],$enl_gotittoo);
          else preg_match_all("/(.+)-(.+)/",$match[1],$enl_gotittoo);
          if ($enl_gotittoo[0]) {
            $album = (!$ENLARGEITSET['enl_sefmode']) ? rtrim($enl_gotittoo[1][0],"&") : $enl_gotittoo[1][0];
            $cat = 0;
            $pos = (!$ENLARGEITSET['enl_sefmode']) ? rtrim($enl_gotittoo[2][0],"&") : $enl_gotittoo[2][0];
          }
          else
          {
            $album = '';
            $cat = 0;
            $pos = 0;
        }

This way I hoped to get $album, $cat and $pos out of the links. But this doesn't work in all cases. Is there a better and more reliable way?

TIA
Timo
Title: Re: preg_match to get URL
Post by: Timos-Welt on June 21, 2008, 01:13:26 pm
I think I've fixed it. Had to learn some stuff about „greedy“ regexp. This one works as expected:

Code: [Select]
  foreach($treffer as $match) {

        if (!$ENLARGEITSET['enl_sefmode']) preg_match_all("#album=(.+)&cat=(.+)&pos=(.+)#i",$match[1],$enl_gotit);
        else preg_match_all("#(.+?)-(.+?)-(.+)#i",$match[1],$enl_gotit);
 
        if ($enl_gotit[0]) {
          $album = $enl_gotit[1][0];
          $cat = $enl_gotit[2][0];
          $pos = $enl_gotit[3][0];
        }
        else
        {
          if (!$ENLARGEITSET['enl_sefmode']) preg_match_all("/album=(.+)&pos=(.+)/",$match[1],$enl_gotittoo);
          else preg_match_all("/(.+?)-(.+)/",$match[1],$enl_gotittoo);
          if ($enl_gotittoo[0]) {
            $album = $enl_gotittoo[1][0];
            $cat = 0;
            $pos = $enl_gotittoo[2][0];
          }
          else
          {
            $album = '';
            $cat = 0;
            $pos = 0;
          }