Tricky php regex....

Forum: LXer Meta ForumTotal Replies: 4
Author Content

Jul 18, 2008
7:11 AM EDT
Ok, as usual I'm working on the digital sign system (as with most corporate projects, this has changed many times as I've worked on it "Hey, it should do this...."). Anyhow, right now we have the ability to put not-yet processed files into the playlists then hit a button to process them all. This seems to be working well.

The tricky thing is fixing the array when it sees an item has been processed. For plain videos and normal images this wasn't too hard (for images you just need to fix the path and change the extension - easy with regex).

The problem comes with the duration coded images. The filename syntax is +number+whatever.jpg +number+ is the duration code.

So what I'm having trouble with is figuring out how to catch that with eregi.

Basically, I need to check if the file (i.e. array item...i.e. variable) starts with that and then strip it off. It was easy enough to do for extensions, but the +'s and the digit (which could theoretically be any length) gets me lost.

Any suggestions?

I can post the code for processing the file extension out if that would help at all.

Thanks guys/gals/bots!

Jul 18, 2008
9:24 AM EDT
Not certain I understand fully your needs, but I tend to use the preg_match ().

First get to the array item you need if you are looping and the first argument is the pattern:

/ ^[pattern]/

The "^" means begins with followed by the pattern you want to match, i.e. php uses the perl syntax as shown.

That is followed by the string it is being tested. If you want to strip it out I would begin with an if condition. That is, if there is a match you use a substring that begins at the end of the pattern you have matched. I think the count begins at 0 (check me on this) so you retain everything from n to the end. You have to do a strlen($filename) first.

$shorter_name = substr ( $filename , n-1 , strlen($filename) ); // don't forget semi colon at // the end of each command


Write to me directly if this is not good enough to get you started. I have an article to write and an important errand. I will check back to see if you dropped me a note later in the day. I hope that helps a bit.


Jul 18, 2008
10:00 AM EDT
Something like this: $newfilename = eregi_replace('^\+[0-9]+\+(.*\.jpg)$', '\\1', $filename);

This will strip off +number+ if the $filename starts with that, otherwise will return the original $filename as such. Is this what is required?

Jul 18, 2008
12:22 PM EDT
Quoting:the +'s and the digit (which could theoretically be any length) gets me lost.

Easy. Escape them with a in front. A . matches any character. \. only matches a dot. \+ matches a plus sign, \\ matches a backslash, etcetera.

Note: If you use double quoted strings in PHP, double up your backslashes! E.g:

"\\.foobar\\" equals '\.foobar\'

That's because a single is a special character in double quoted strings (e.g. \n for newline, \t for tab) but not in a single quoted string. Try this:

echo "foo\nbar\n"; echo 'foo\nbar\n';

Edit: Hehe. It seems LXer also uses \ for escape. In order to write the double backslash \\ I had to write \\\\ :-)

Jul 18, 2008
3:22 PM EDT
Something like what krisum said. I got the answer on IRC shortly after posting and got busy coding/testing and forgot to reply here. . :)

Thanks as usual guys!

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!