Advanced search  

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Pages: [1]   Go Down

Author Topic: [Solved]: preg_match to get URL  (Read 9601 times)

0 Members and 1 Guest are viewing this topic.

Timos-Welt

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 808
    • Timos-Welt
[Solved]: preg_match to get URL
« 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
« Last Edit: June 22, 2008, 12:08:43 pm by Joachim Müller »
Logged

Timos-Welt

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 808
    • Timos-Welt
Re: preg_match to get URL
« Reply #1 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;
          }
Logged
Pages: [1]   Go Up
 

Page created in 0.025 seconds with 19 queries.