www.webdeveloper.com
Results 1 to 5 of 5

Thread: Some Regular expresstion

  1. #1
    Join Date
    Nov 2012
    Location
    Iran
    Posts
    5

    Question Some Regular expresstion

    hi, i don't know how write regexp (preg_match() in php) and i wanna following regexps:
    1. just number
    2. just words
    3. just words, numbers, -._
    tanks.

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,159
    Code:
    $reg1 = preg_match("/[0-9]/", "String to check");  // Only numbers
    $reg2 = preg_match("/[a-z]/i", "String to check");  // Only letters
    $reg3 = preg_match("/[a-z0-9-._]/i", "String to check");  // numbers, letters and -._
    There are a few different ways to use those regular expressions but I laid out a simple preg_match function stored in each variable above.
    Last edited by Sup3rkirby; 11-14-2012 at 02:53 AM.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    Those checks are all well and good, if all you want to know if is the string contains "a letter" , "a number" or "a letter, number, hyphen, period, or underscore".

    I'm going to assume this is for validating some type of login or password?

    If that's the case, you probably want to check for things OTHER than what you want.

    Take a look at these examples:

    PHP Code:
        $test_string "Str1ng-t0_ch3ck.";
        
    $test_string_words "Stringtocheck";
        
    $test_string_numbers "12345";
        
    $test_string_mixed "Str1ng t0 ch3ck.";    // note the added spaces; need to add \s to the regex patterns to match this
        
        
    $reg1 preg_match("/[^0-9]/"$test_string);  // Only numbers
        
    $reg1a preg_match("/[^0-9]/"$test_string_numbers);  // Only numbers

        
    $reg2 preg_match("/[^a-z]/i"$test_string);  // Only letters
        
    $reg2a preg_match("/[^a-z]/i"$test_string_words);  // Only letters
        
        
    $reg3 preg_match("/[^a-z0-9-._]/i"$test_string);  // numbers, letters and -._

        
    $reg4 preg_match("/[^\w-._]/i"$test_string_mixed);  // numbers, letters and -._    
        

        /*
        preg_match returns int(1) if it finds a match to the pattern or int(0) if it does not find a match.
        since these checks are checking for NOT IN THE PATTERN, then (if $var === 0) "passes" the regex and (if $var === 1) "fails"
            {three "equal signs" matches exactly the type int()} 
        ------------ reg1 (it found something other than just numbers)
        1
        ------------ 
        ------------ reg1a (it found only numbers)
        0
        ------------ 
        ------------ reg2 (it found something other than just letters)
        1
        ------------ 
        ------------ reg2a (it found only letters)
        0
        ------------ 
        ------------ reg3 (it found only the acceptable characters)
        0
        ------------ 
        ------------ reg4 (condensed "word boundries (letter and numbers) plus the `extra characters`")
        1
        ------------ 
        
        */ 

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,284
    Also, please see the various ctype_*() functions for more efficient ways to do certain checks.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Nov 2012
    Location
    Iran
    Posts
    5

    Question Just

    i want check input and
    1.if it was just numbers return 1 and if it has characters return 0.
    2.if it was just characters return 1 and if it has numbers return 0.
    3.if it was just characters and numbers return 1 and if it has else characters line &,*,... return 0.
    3.if it was just characters and numbers and -_. return 1 and if it has else characters line &,*,... return 0.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14721 seconds
  • Memory Usage 2,890KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates