• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Regular Expressions

Hey there, well I'm horrible at regular expressions.

Basically I am trying to do the following:

I have 3 video files as an example.  Each file is named slightly differently some files have the format

_clip_
_clip
clip_

looks like
(randomtext)_clip_(randomtext)
(randomtext)_clip(randomtext)
(randomtext)clip_(randomtext)

I would just like to store them appropriately into the array sets that they belong to.  I attached some of the code I am using, but again the regular expressions aren't there because I am really bad at it.
<?php
 
//Example:
/*
 * 
 * bobsgreatadventure_clip1_56k.wmv
 * bobsgreatadventure_clip156k.wmv
 * bobsgreatadventureclip1_56k.wmv
 * 
 */
 
// First look if we have clip in the code
if(strpos($minifile, 'clip'))
{
    // Check to see if it's _clip_
    if(preg_match('_clip_', $minifile))
    {
        $set_one[] = $minifile;
    }
    // Check to see if it's _clip
    elseif (preg_match('_clip', $minifile))
    {
        $set_two[] = $minifile;
    }
    // Check to see if it's clip_
    elseif (preg_match('clip_', $minifile))
    {
        $set_three[] = $minifile;
    }
}
 
?>

Open in new window

0
Azurewave
Asked:
Azurewave
  • 6
  • 4
  • 3
2 Solutions
 
b0lsc0ttIT ManagerCommented:
Azurewave,

What type of variable is $minifile ?  Does it just have the file name and you are just "testing" for the match?

Try ...

if (preg_match('/_?clip_?/', $minifile, $regs) {
    $match = $regs[0];
} else {
    $match = "";
}

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
ddrudikCommented:
Azuerwave, how can these files be named?  Your question mentions _clip_ but your example shows _clip1_

Will the files contain other characters  other than _clip_ that you want to test?  Please be as specific as possible since that will cause the regex pattern supplied to change.

In any case, here's some test code that works against your current sample data:
<?php
function checkminifile($minifile) {
  global $set_one,$set_two,$set_three;
  // First look if we have clip in the code
  if(strpos($minifile, 'clip'))
  {
 
    // Check to see if it's _clip_
    if(preg_match('/_clip1_/', $minifile, $temp))
    {
        $set_one[] = $minifile;
    }
    // Check to see if it's _clip
    elseif (preg_match('/_clip1(?!=_)/', $minifile, $temp))
    {
        $set_two[] = $minifile;
    }
    // Check to see if it's clip_
    elseif (preg_match('/(?<!_)clip1_/', $minifile, $temp))
    {
        $set_three[] = $minifile;
    }
  }
}
echo '<pre>';
checkminifile('bobsgreatadventure_clip1_56k.wmv');
echo '$set_one '.print_r($set_one,true).'<br>';
checkminifile('bobsgreatadventure_clip156k.wmv');
echo '$set_two '.print_r($set_two,true).'<br>';
checkminifile('bobsgreatadventureclip1_56k.wmv');
echo '$set_three '.print_r($set_three,true);
?>

Open in new window

0
 
AzurewaveAuthor Commented:
Ah yes, sorry about that.

The clip is followed by a random number.

_clip(number)_

it's a string, and it would always be something like

_clip(number)_
_clip(number)
clip(number)_

the string before and after "_" could be anything.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
AzurewaveAuthor Commented:
better example

(randomtext)_clip(number)_(randomtext)
(randomtext)_clip(number)(randomtext)
(randomtext)clip(number)_(randomtext)

I completely forgot about the number thank you for pointing that out.
0
 
b0lsc0ttIT ManagerCommented:
Good catch Ddrudik and that was an important piece of info left out Azurewave.  Thanks for clarifying.

My suggestion would need to be changed to ...

if (preg_match('/_?clip\d+_?/', $minifile, $regs) {
    $match = $regs[0];
} else {
    $match = "";
}

Let me know how that works or if you have a question.

bol
0
 
b0lsc0ttIT ManagerCommented:
As I reread this I realize my suggestion is over simplified and not what you want.  You want the filename, not just the clip part, and need them organized into groups.  Sorry I overlooked that.

Try ...

{
    // Check to see if it's _clip_
    if(preg_match('/_clip\d+_/', $minifile))
    {
        $set_one[] = $minifile;
    }
    // Check to see if it's _clip
    elseif (preg_match('/_clip\d+/', $minifile))
    {
        $set_two[] = $minifile;
    }
    // Check to see if it's clip_
    elseif (preg_match('/clip_\d+/', $minifile))
    {
        $set_three[] = $minifile;
    }
}

If the step in the If (i.e. $set_NNN[] = $minifile;) is suppose to fill an array then I can help you with that part too.

Let me know if you have a question or need more info.

bol
0
 
AzurewaveAuthor Commented:
thanks b0lsc0tt, everything seems to be coming into play however

    // Check to see if it's _clip
    elseif (preg_match('/_clip\d+/', $minifile))
    {
        $set_two[] = $minifile;
    }

that part of the code is getting thorugh the filter.

i run it and this
bobsgreatadventure_clip1qt.mpg

goes astray, any ideas?
0
 
AzurewaveAuthor Commented:
nvm, ignore my last reply i think i figured out the issue.

Give me a bit, and I'll report back with news.
0
 
AzurewaveAuthor Commented:
alright i found out another isse:

for:

preg_match('/_clip\d+/',

on this, it needs to make sure that the character that follows is a number.

ex: bobsadventure_clip4qt.wmv

I have some files that are
bobsadventure_clipqt.wmv

that I don't want in the system at all thats why they should'nt make it into the arrays.
0
 
ddrudikCommented:
Azurewave, try this code and report:
<?php
function checkminifile($minifile) {
  global $set_one,$set_two,$set_three,$set_bad;
  // First look if we have clip in the code
  if(strpos($minifile, 'clip'))
  {
 
    // Check to see if it's _clip_
    if(preg_match('/_clip\d+_/', $minifile, $temp))
    {
        $set_one[] = $minifile;
    }
    // Check to see if it's _clip
    elseif (preg_match('/_clip\d+(?!=_)/', $minifile, $temp))
    {
        $set_two[] = $minifile;
    }
    // Check to see if it's clip_
    elseif (preg_match('/(?<!_)clip\d+_/', $minifile, $temp))
    {
        $set_three[] = $minifile;
    }
    else {
        $set_bad[] = $minifile;
    }
  }
}
echo '<pre>';
checkminifile('bobsgreatadventure_clip1_56k.wmv');
checkminifile('bobsgreatadventure_clip156k.wmv');
checkminifile('bobsgreatadventureclip1_56k.wmv');
checkminifile('bobsgreatadventureclip156k.wmv');
echo '$set_one '.print_r($set_one,true).'<br>';
echo '$set_two '.print_r($set_two,true).'<br>';
echo '$set_three '.print_r($set_three,true).'<br>';
echo '$set_bad '.print_r($set_bad,true).'<br>';
?>

Open in new window

0
 
AzurewaveAuthor Commented:
Yep that was it, thanks guys.
0
 
b0lsc0ttIT ManagerCommented:
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
 
ddrudikCommented:
Thanks for the question and the points.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now