Not a Member Yet,
Click here to Register

ID: 490
Viewed: 27031
Added: Sep 09, 2002
Version:
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 www.htmlforums.com and I'll pop along and sort them out.

<!---Head--->
none

<!---Body--->


 <!--- START COPY AND PASTE FOR CUSTOM TAG --->
<!---
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
--->
<cftry>

<!--- 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 --->
<cfscript>
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]));
}
}
</cfscript>
<!--- End of Perform function --->
<cfelse>
<!--- Not all the required attributes were passed through <cfmodule> --->
<cfthrow
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*" />
</cfif>

<!--- Error handler --->
<cfcatch type="UndefinedAttributes">
<cfoutput>#cfcatch.detail#</cfoutput>
</cfcatch>

</cftry>
<!--- END CUSTOM TAG COPY AND PASTE --->

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

<!--- Simple Example --->
<cfmodule template="cust_PregMatch.cfm"
patte\r\n="([a-zA-Z]+)[-]([0-9]+)"
line="ABC-123"
/>
<!--- OUTPUT RESULTS --->
<cfset maxLoop=arrayLen(preg_match) />
<cfloop index="idx" from="1" to="#maxLoop#">
<cfoutput>Preg_Match<[#idx#] = [#preg_match[idx]#]
</cfoutput>
</cfloop>

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

<!--- 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"
patte\r\n='(S+)s(S+)s(S+)s[[](.+)[]]s["](.+)["]s(S+)s(S+)s["](.*)["]s["](.+)["]'
line="#arrFile[1]#"
/>

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

</body>
</html>


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 snippetlibrary.com All Rights Reserved. Conditions
Do NOT follow this link or you will be banned from the site!