[PHP] Custom BB codes, with a twist. Need some help.

scholzie

Gawd
Joined
Feb 15, 2003
Messages
633
I am scripting a parsing program for article formatting on my site, but I need to do some stuff that I can't figure out easily.

I want to take a variable image name (the actual name is up to the author of the article), such as 001.jpg, and using the format
Code:
%001.jpg%
have php look for text matching that format. When it finds it, I want it to use the file name (001.jpg in this case) as the value for a variable "$image", and the image's name with 't' (such as 001t.jpg) appended as "$thumbnail".

The final output should be something like:
Code:
<a href=001.jpg><img src=001t.jpg></a>

I was going to make a $search and $replace variable set and use preg_replace() to search for the % symbols and parse accordingly, but I dont use regular expressions enough to know how to use it properly.

Any help would be majorly appreciated - thanks in advance.
 
Code:
$content = preg_replace("!%(\w+)\.(\w+)%!i","<a href=\"$1.$2\"><img src=\"".$1."t$2\"></a>",$content);
or along those lines, untested, this should give an idea. it may be easier to make your special tags the actual bb tags, as in [ img][/img ] (without spaces)
 
Originally posted by tim
Code:
$content = preg_replace("!%(\w+)\.(\w+)%!i","<a href=\"$1.$2\"><img src=\"".$1."t$2\"></a>",$content);
or along those lines, untested, this should give an idea. it may be easier to make your special tags the actual bb tags, as in [ img][/img ] (without spaces)

Thanks a bunch, Tim. Any chance you could explain what all that does, or point me somewhere that will tell me? I'd like to know what I'm doing so I know how to fit it to my needs, as well as make more tags as I need them.
 
Thanks for your replies Tim - I've been trying to get it, but I can't seem to pound it into my head. I suppose I'll have to either find another way to do it, or figure out a way to learn regexp a little better.
 
Ok, I finally understand that
Code:
%(\w+)\.(\w+)%
will pick up things like %book.jpg% and not %book%, which is good. I also understand that $1 and $2 will be my two backreferences from the search.

Now, what I dont get is why you've done
Code:
!%(\w+)\.(\w+)%!i

What is the reason for the two ! marks and the 'i' at the end? Is the 'i' there to make the search case-insensitive, or is the '!i' there to make sure it is case sensitive?

I think i'm slowly picking up how it works, although the ! and 'i' are confusing me.


EDIT: Nevermind - I played around with the regexp a bit and I see what the !'s do - I appreciate the help Tim, it did what I needed it to do, and I even understand it!
 
yeah, the !'s are just what i like to use as delimiter thingies. many examples use '/' but you then need to escape them in the regex, so far i've never needed to use a ! in a regex. the i does make it case insensitive (though it doesn't really matter for this one).

there are other functions that use regex style searching but don't need delimiters, i'm thinking of ereg/eregi

another thing you could do is replace the 2nd (\w+) with (jpg|gif|png|bmp) plus any other extensions you would want (in this case you would want the i at the end to make sure it's case insensitive)
 
tim, that gives me an idea -

what expression could I use to find all things formatted like %image.jpg%, but are not matched in the (jpg|gif|png|bmp) expression? Basically, I'd like to catch things like %something.zip% and let the user know the file format isnt supported.
 
hey, cool site, just curious, does it use some kind of portal or cms?
 
Originally posted by teck9
hey, cool site, just curious, does it use some kind of portal or cms?
to which site are you refferring?
 
Originally posted by tim
to which site are you refferring?


www.PimpedOutCases.net , looks ilek a cool site the way its setup is similar to how im trying to set up my site except instead of pictures of cases, itd be pictures of people:D so im not sure if its a portal or cms or straight up coding from scratch that was used to create the site

thanks

--teck9
 
Its using a CMS called e107 until I finish the CMS I'm coding.

The script I'm coding now is actually going to be for formatting articles in our specific article formatting scheme so that we don't have to do it by hand anymore.
 
Back
Top