News:

CPG Release 1.6.29
During HTML5 upload, keep pseudo blank code 200 messages from triggering error condition
added Russian language
correct failure to use theme menu icons in album manager
minor vulnerabilities mitigation

Main Menu

[Solved]: preg_match to get URL

Started by Timos-Welt, June 20, 2008, 12:40:23 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Timos-Welt

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):


  // 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]:


  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

Timos-Welt

I think I've fixed it. Had to learn some stuff about ,,greedy" regexp. This one works as expected:


  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;
          }