Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number Extraction Query

Status
Not open for further replies.

403124

Programmer
Joined
Dec 2, 2005
Messages
2
Location
GB
Hi,

ColdFusion newbie, converting from PHP to CF!

Anyway I have query which I'm stuck on.

What I have is the following:

cat_name=Holiday-Guides-9

But I would like to extract the number so I get 9, I've had a go, but I get 1 which is not correct. How do I go about getting 9?

Here's my version:

<cfset set_catid=ReFindNoCase("[^0-9]*","#url.cat_name#")>

<cfoutput>
#set_catid#
</cfoutput>

Thanks

Oz.
 
403124, is this what you were looking for:
Code:
<br/>=============================================<br/>
<cfset cat_name = "Holiday-Guides-9">
<cfset set_catid = #REFindNoCase("[0-9]",cat_name,1,"True")#>

<cfoutput>
  #mid(cat_name,set_catid.pos[1],set_catid.len[1])#
</cfoutput>
<br/>=============================================<br/>

The above will return the 9. Replace the 9 with a 7, and the above will return the 7, and so forth...

Also look at:


____________________________________
Just Imagine.
 
Thanks GUJUm0deL,

That worked a treat.

I will have a look at the livedocs page and get myself familiar with it all, especially using the pos and len attributes, something I will using in the future.

Cheers

Oz.
 
There's a one-liner way to do it.. (Besides setting cat_name).

Code:
<cfset cat_name = "Holiday-Guides-9">
<cfset set_catid = listLast(cat_name,"-")>

As you may have guessed, this method treats the string cat_name like a "-" delimited list.

There's only one reason that this may work better.. I think the regex above would find the first number in the string.. So if the category name happened to contain a number, it would find it.

Anyway, just a suggestion.

With a similar method, you can also retrieve the category name easily..

Code:
<cfset cat_name = "Holiday-Guides-9">
<cfset set_catid = listLast(cat_name,"-")>
<cfset set_catname = listDeleteAt(cat_name,"-")>

This works by deleting the last element from the '"-" delimited list'

Have a good day.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top