I'm just starting to get to grips with regular expressions, but I'm
flailing about somewhat with the concept & syntax.
I'd like some back-to-basics advice to help me get to grips with
building reg ex.
Today I was trying to use preg_replace() to strip non-alphanumeric
chars from a string.
The basic purpose was to generate a filename based on a database record
& thus associate an uploaded file with that particular db record.
I'll explain in detail, but you may wish to skip below to the crux
(after the ------)
Please note that I ONLY need help in understanding reg ex & how to use
preg_replace() & similar functions. I don't need help scripting the
situation described below. It's only the reg ex part that is a gap in
my knowledge.
--- follows is an explanation, you may skip this
----------------------------------
EG: sample db record
(id, cTitle, cDescription)
(1, "Leadership Skills", "This 2 day course is designed to....")
The admin user selects a PDF file to upload & then selects a "Course"
from a dropdown list to associate the PDF to.
The users PDF may be called "SFL Leadership & Management #3.pdf" but I
need to generate a new file name based on the db fields: id & cTitle.
I'm aiming for something like "1_leader_1.pdf"
Where the following naming convention applies:
(id)_(1st 6 chars of cTitle in lowercase with non-alphanumeric chars &
SPACE & _ stripped)_(number).pdf
[where (number) indicates that this is the 1st, 2nd or 3rd document to
be associated with this particular course]
OK, so far?
Right, so my approach has been to setup the NEW NAME for the PDF in the
VALUE attribute of the dropdown list.
<option value="1_leader">Leadership Skills</option>
Then before uploading I can scan the dir for files starting with
"1_leader" & append the filename with _2.pdf, _3.pdf as appropriate.
-----------------------------------------------------------------------
For the reg ex I tried this first.
preg_replace("/^\W+\s+_+/","", $cTitle);
So now I will be showing my newbie-ness to reg exp, as all the /^\+\$
stuff is pretty much alien to me - This is what I need explaining.
I did manage to get it working afterwards though by using the following:
$exp=array('/\W/','/\s/','/\_/');
preg_replace($exp,"", $cTitle);
Is my 1st attempt a PATTERN? & my 2nd a FILTER?
but i'm still very confused by all the symbols.
I understand this:
\W is non-word - so that should strip everything that is not [a-z]
[A-Z] [0-9]
\s is spaces
& I can look up all the other Meta Chars on php.net
What I don't understand is how to build a reg ex.
/ something /
..so the //'s delimit the reg ex?
^ starts something, but what does it start?
$ ends something, but what does it end?
oh dear, im getting confused again. I need someone to talk me though
this please.
Thank you & sorry for this very long post!
Riquez
Community email addresses:
Post message: [email protected]
Subscribe: [EMAIL PROTECTED]
Unsubscribe: [EMAIL PROTECTED]
List owner: [EMAIL PROTECTED]
Shortcut URL to this page:
http://groups.yahoo.com/group/php-list
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/php-list/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/