www.webdeveloper.com
Results 1 to 6 of 6

Thread: Having A Little Regular Expression Issue...

  1. #1
    Join Date
    Sep 2008
    Posts
    260

    Having A Little Regular Expression Issue...

    Eventually I'll sharpen up on these.

    I have a piece of code that loops through a collection of fields. I want it to "skip" a field if the condition I set is true. My code is working for all other conditions except this one.

    Let's say I have three fields "first name", "last name", and "username". I want it to skip a field if the field name is "username".

    I thought this should work:

    PHP Code:
                               if ( preg_match('/username/i'$field_name bp_get_the_profile_field_name() ) ) {
                        
                                   global 
    $profile_template$field;
                                
                                   
    $field $profile_template->next_field();
                                
                               } 

    But instead it's getting rid of all three fields with "name" in it. I've tried all kinds of combinations as well. Any idea on the reg exp notation I need to use?

  2. #2
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    1. try to debug this, for example, by outputting the value of $field_name. make sure preg_match is actually evaluating what you expect it to be evaluating.
    2. globals are bad practice. try to rewrite your code in such a way that you don't need them anymore.
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  3. #3
    Join Date
    Sep 2008
    Posts
    260
    Quote Originally Posted by themarty View Post
    1. try to debug this, for example, by outputting the value of $field_name. make sure preg_match is actually evaluating what you expect it to be evaluating.
    2. globals are bad practice. try to rewrite your code in such a way that you don't need them anymore.
    I really would want to avoid using a global here, but I have no choice. This is a Wordpress API that I'm customizing.

    I "know" that the variable $field_name works because it works here:

    PHP Code:
                               if ( preg_match('/Date(\s+)Of(\s+)Birth/i'$field_name bp_get_the_profile_field_name() ) ) {
                        
                                   global 
    $profile_template$field;
                                
                                   
    $field $profile_template->next_field();
                                
                               } 
    where it looks for the field "Date Of Birth" and it skips to the next field...

    I think the issue has to do with the regular expression if anything..

  4. #4
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    If you are so sure of what the value is, simply create a test-script that outputs the result of that regular experssion.

    p.e.

    PHP Code:
    if ( preg_match('/username/i''first name' ) ) {
      echo 
    "match";
    } else {
      echo 
    "no match";

    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    My sneaky suspicion is that something in the logic is wrong, but since I don't know what any of those method calls actually does, I can't be sure, but my spidey senses make me wonder if the next_field() is moving an array or result row pointer before returning the value, and actually giving you the next row, which might be empty, and therefore is returning null or false. But that's pure speculation on my part for now.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Sep 2008
    Posts
    260
    Quote Originally Posted by themarty View Post
    If you are so sure of what the value is, simply create a test-script that outputs the result of that regular experssion.

    p.e.

    PHP Code:
    if ( preg_match('/username/i''first name' ) ) {
      echo 
    "match";
    } else {
      echo 
    "no match";

    good suggestion....in the meantime, I just created a little JavaScript temporary solution that pulls the field I don't want in the form.

    @NogDog

    ...it could be returning an empty row and as a result skipping to the next...it is kind of weird....I'll look into it more...thanks for your input..

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



Recent Articles