Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts

    search variable for string

    Hello
    I am trying to test a string variable to see if it contains another string.

    Actually I want to test two at once

    At the moment my script it test to see if the variables are empty:

    PHP Code:
    if (!empty($N_contact1) && !empty($N_email1)) {
          
    $N_contactx $N_contact1;
            
    $N_emailx $N_email1;
            
    $ref_cnt 1;
            
            require_once(
    "a_inserter.php");
            } 
    // end if 
    Now instead of empty I want to test for if:
    $N_contact1 contains "Enter Name"
    and
    $N_email1 contains "Enter Email address"

    I thought of using

    PHP Code:
    $pos strpos($mystring$findme);

    if (
    $pos === false) {
        echo 
    "The string '$findme' was not found in the string '$mystring'"
    But maybe this is going to be a bit clumsy for both variables.

    Any better ideas ?


    Thanks
    Last edited by jeddi; 12-14-2008 at 08:52 PM. Reason: make it better :)
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Try the string search function

    http://us2.php.net/manual/en/function.strpbrk.php

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks
    I read that.

    So is this gonna do the job ?

    PHP Code:
    if (strpbrk($N_contact1'Enter Name') && strpbrk($N_email1'Enter Email address')) {
          
    $N_contactx $N_contact1;
            
    $N_emailx $N_email1;
            
    $ref_cnt 1;
            
            require_once(
    "a_inserter.php");
            } 
    // end if 
    Hope I understood it ok
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I don't believe that strpbrk is what you want to use. I haven't once used this function, though it appears that its purpose is to find any character from char_list in the provided haystack and return the remaining string from the first match.
    Use stripos instead (or strpos if you're case is needing to be sensitive), which was you're original idea. This function arguments are the same as strpbrk, but looks for position location of strings. That being said, if it matches in the first location, it should be a 0, which also matches to false. So, you need to convert to using identical operators:
    PHP Code:
    if (false !== stripos($N_contact1'Enter Name') || false !== stripos($N_email1'Enter Email address'))
    {
        
    // This is if either of them contain the default data.
        // Flip the ! and use && if you want to confirm they do not contain the default data.

    Further than this, standard evaluation techniques will get around the email. Since 'Enter Email Address' isn't a valid email address, that can be caught with a simple pattern match. Actually, you could do both of these with pattern matching.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks
    so using this ...

    PHP Code:
    if (false !== stripos($N_contact1'Enter Name') || false !== stripos($N_email1'Enter Email address'))

          
    $N_contactx $N_contact1;
            
    $N_emailx $N_email1;
            
    $ref_cnt 1;
            
            require_once(
    "a_inserter.php");
            } 
    // end if 
    My next bloc will be:

    PHP Code:
    if (false !== stripos($N_contact1'Enter Name') || false !== stripos($N_email1'Enter Email address'))

          
    $N_contactx $N_contact2;
            
    $N_emailx $N_email2;
            
    $ref_cnt 2;
            
            require_once(
    "a_inserter.php");
            } 
    // end if 
    And so on up to number 10

    So rather than write it out ten times would this be correct ?

    PHP Code:
    for ($i=1;$i=10;$i=$i +1){

        if (
    false !== stripos($N_contact.$i'Enter Name') || false !== stripos($N_email.$i'Enter Email address')) { 
          
    $N_contactx $N_contact.$i;
            
    $N_emailx $N_email.$i;
            
    $ref_cnt $i;
            
            require_once(
    "a_inserter.php");
        } 
    // end if

    // end for 
    Is that dot in "$N_contact.$i" the right way to do it ?
    Thanks for your help.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    Regular Coder lokeshshettyk's Avatar
    Join Date
    Aug 2008
    Location
    On the way to the moon!
    Posts
    157
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Yeah, Dot should be fine

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by jeddi View Post
    Thanks
    so using this ...

    PHP Code:
    if (false !== stripos($N_contact1'Enter Name') || false !== stripos($N_email1'Enter Email address'))

          
    $N_contactx $N_contact1;
            
    $N_emailx $N_email1;
            
    $ref_cnt 1;
            
            require_once(
    "a_inserter.php");
            } 
    // end if 
    That depends on what you're processing is supposed to be. From the looks of it, you'll want to be confirming that they do not exist, so you'd use false === for both checks and combine them with an &&. That way, you'll have (logically): if notinstr AND notinstr, which is true and true if the values have been changed.

    My next bloc will be:

    PHP Code:
    if (false !== stripos($N_contact1'Enter Name') || false !== stripos($N_email1'Enter Email address'))

          
    $N_contactx $N_contact2;
            
    $N_emailx $N_email2;
            
    $ref_cnt 2;
            
            require_once(
    "a_inserter.php");
            } 
    // end if 
    And so on up to number 10

    So rather than write it out ten times would this be correct ?

    PHP Code:
    for ($i=1;$i=10;$i=$i +1){

        if (
    false !== stripos($N_contact.$i'Enter Name') || false !== stripos($N_email.$i'Enter Email address')) { 
          
    $N_contactx $N_contact.$i;
            
    $N_emailx $N_email.$i;
            
    $ref_cnt $i;
            
            require_once(
    "a_inserter.php");
        } 
    // end if

    // end for 
    Is that dot in "$N_contact.$i" the right way to do it ?
    Thanks for your help.
    The loop definition is incorrect. It will only loop if you're $i is equal to 10, so you want to use you're definition like so:
    for ($i = 1; $i <= 10; ++$i)
    Not sure about the $i on that one.
    Where does $N_contact and $N_email come from? The $i would work if you have you're variables like so:
    $N_contact1
    $N_contact2
    $N_contact3
    ...

    If its an array, you'd index into you're array with the $i: $N_contact[$i].
    So, this all depends on you're definition of $N_contact* and $N_email*.

    Edit:
    On second thought, would that $i work tacked on the end? I'm trying to remember for certain how that works (without being able to test it). I tend to use arrays for these situations to avoid these troublesome variable creations. Since I tend to do it this way, I can't confirm 100% that the $var . $i works maps as a $var1. You may need to consider variable variables for this type of situation, which is going to get messy.
    Last edited by Fou-Lu; 12-15-2008 at 07:05 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for the input.

    I am testing the input from a form.

    The initial text in the "input field" is "Enter Name - 1"
    There are 10 input fields.
    For the email it is "Enter Email address - 1"

    the input data are extracted from the post and put into variables:

    $N_contact1 thru to $N_contact10
    (same with email addresses)

    So the logic must be:
    If $N_contact1 does not contain "Enter Name" AND $N_email1 does not "Enter Email address", then process.

    If the field doesn't contain the origonal text then I assume they filled it in

    It is true that I need to validate the email address before sending an email but thats ok.

    So now I've explained it more,

    should that OR ( || ) be changed to an "&"
    in : ?

    PHP Code:
    if (false !== stripos($N_contact1'Enter Name') || false !== stripos($N_email1'Enter Email address')) 
    any thoughts ?
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yeah, I think I mentioned this in my last post. You'll need to check for false === on both, and combine them with an && to evalulate them. This would say that its not in the string and not in the string, which sounds like what you want to do.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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