Not a Member Yet,
Click here to Register

ID: 490
Viewed: 27109
Added: Sep 09, 2002
Snippet uploaded by: torrent
Written By: torrent
Demo: Sorry, no demo

User Rated at: 5 Stars5 Stars5 Stars5 Stars5 Stars
Rate This:

Thank you for your vote. Please wait...

It appears you already voted for this snippet

It appears your vote value was empty

This is a custom CFML tag which will return a pattern matched string to an array, with each backreference occupying an element of the array and the whole matched string occupying the first element.. This emulates the preg_match() function of PHP.

Instructions are to copy and paste the custom tag code a file. Copy this file to either the folder from where it will be invoked or the custom tag folder. View the samples which show how the tag should be used. Any questions, post them on and I'll pop along and sort them out.



Description: A custom tag to perform the equivalent of PHP's preg_match() function.
This will take a regular expression along with a string and retu\r\n any
backreferences in an array.

preg_match[1] = the complete patte\r\n-matched string
- FALSE is retu\r\ned if no match is found
- *ERROR REPORTED* is retu\r\ned if the operation fails
preg_match[2...n] = strings retu\r\ned through the backreferences.

Entering: N/A
Exiting: N/A

Dependencies: N/A
Expecting: patte\r\n : the regular expression patte\r\n
line : the string against which the patte\r\n will be compared

Modification History:
Date Modifier Change
07-Sep-02 Torrent Created

<!--- Check if variables are defined --->
<cfif isDefined("attributes.patte\r\n") and isDefined("attributes.line")>
<!-- Initialise the preg_match array -->
<cfset caller.preg_match = arrayNew(1) />
<cfset caller.preg_match[1] = FALSE />
<!-- Nuff talk, let's get down to business --->
if (REFind(attributes.patte\r\n, attributes.line, 1)) {
match = REFind(attributes.patte\r\n, attributes.line, 1, TRUE);
maxLoop = arrayLen(match.pos);
for (idx = 1; idx lte arrayLen(match.pos); idx = idx + 1) {
// Note: The toString() is required in case a match retu\r\ns -1, which could
// otherwise be interpreted as FALSE in the calling template
caller.preg_match[idx] = toString(mid(attributes.line, match.pos[idx], match.len[idx]));
<!--- End of Perform function --->
<!--- Not all the required attributes were passed through <cfmodule> --->
type ="UndefinedAttributes"
detail="<b>Missing required attributes</b>

Required attributes are:
patte\r\n (type = string, content = regular expression)

line (type = string, content = the string to perform the regular expression against)
<cfset caller.preg_match[1] = "*ERROR REPORTED*" />

<!--- Error handler --->
<cfcatch type="UndefinedAttributes">


<!--- START OF SAMPLE CODE (copy to a new file) --->
<title>Sample use for preg_match custom tag</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<!--- Simple Example --->
<cfmodule template="cust_PregMatch.cfm"
<cfset maxLoop=arrayLen(preg_match) />
<cfloop index="idx" from="1" to="#maxLoop#">
<cfoutput>Preg_Match<[#idx#] = [#preg_match[idx]#]

<!--- /////////////////////////////////////////////////////////// --->

<!--- More complex (virtual) example --->
<cffile action="read" file="d:apachelogsaccess.log" variable="test">
<cfset arrFile = listToArray(test, "#chr(13)##chr(10)#") />
<cfmodule template="cust_PregMatch.cfm"

<cfif Preg_Match[1] NEQ FALSE>
<cfset maxLoop=arrayLen(preg_match) />
<cfloop index="idx" from="1" to="#maxLoop#">
<cfoutput>Preg_Match<[#idx#] = [#preg_match[idx]#]
No matches found.


No Comments to show

Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.

Replying to a Comment...

Adding your comment. Please wait...

Thanks for adding your comment!. After further review it will be added.

There was a problem adding your comment. Please try again.

Please complete all the fields in the form before sending.

© 2002 - 2024 All Rights Reserved. Conditions
Do NOT follow this link or you will be banned from the site!