Another PHP/regex question. :)

Forum: LXer Meta ForumTotal Replies: 2
Author Content
techiem2

Dec 12, 2008
1:19 PM EDT
Ok, I've run across another tricky spot in my Sign project. Long story short, I'm replacing an item in an array with the processed filename, and creating a symlink to the processed filename. Now I need to see which array the original item was in for making the symlink properly (i.e. use the right directory in the symlink).

So I have a couple if in_array sets to do this.

For video files, it's no big deal as the filename doesn't change, only the unprocessed file flag is removed, so it's easy to check the array for processed files or unprocessed files. $file is the filename of the item in the hotkey array (i.e. the final filename that will be symlinked to). $content_array is the array holding the available to use processed and unprocessed video files.

Quoting: if (in_array ($file, $content_array)||in_array("* " . $file, $content_array)) { blah blah write symlink using content directory and $file }


For images however, it's a bit trickier. I'm doing all steps on processing, so for images the name in the array will not match the name in the available files array until the file finishes processing.

The image name in the available image files/image videos array could be one of:

"Hi.mpg" (this file is already processed - easy enough)

"* Hi.png/jpg/etc" ("* " is the unprocessed file tag)

"* +somenumber+Hi.png/jpg/etc" (+somenumber+ is the time coding flag - this is removed from the filename when the file is processed into a video)

The file will be listed as "Hi.mpg" in the hotkey array. As you can see it's a bit tricky.

Any suggestions? I'm basically not sure how to go about searching to include the extension regex and time code regex....
penguinist

Dec 12, 2008
4:29 PM EDT
Maybe something in this direction?

$content_array = array("foo.jpg", "*bar.mpg", "*+20081212+foo.mpg");

# get all mpg files $selection = preg_grep("/\.mpg$/", $content_array); if (sizeof($selection) > 0) { // do something with the files }

# get all mpg files with time stamp and *+ flag $selection = preg_grep("/\*\+\d+.*\.mpg$/", $content_array); print_r($selection);
techiem2

Dec 16, 2008
6:46 PM EDT
Thanks! That got me on the right track. Since I already cut the * and ++ off, I figured I could just chop off the extension and the grep for the main filename in the array. It seems to be working well. (there's probably a more elegant way to do this, but it works).

Quoting: //cut extension off filename (make sure we don't use . in any filenames or this may not work right) $linefilename = explode (".", $linestart); //search for the filename in the array $selection = preg_grep("/$linefilename[0]/",$staticcontent_array); print ("Selection is : "); print_r ($selection); if (sizeof($selection) > 0) { // do something with the files fwrite($fp, "$staticcontentdir/$line\n"); }

Posting in this forum is limited to members of the group: [Editors, MEMBERS, SITEADMINS.]

Becoming a member of LXer is easy and free. Join Us!