...

View Full Version : search variable for string



jeddi
12-14-2008, 08:04 PM
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:



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


$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

ptmuldoon
12-14-2008, 10:25 PM
Try the string search function

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

jeddi
12-15-2008, 06:26 AM
Thanks
I read that.

So is this gonna do the job ?


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

Fou-Lu
12-15-2008, 06:41 AM
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:


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.

jeddi
12-15-2008, 06:49 AM
Thanks
so using this ...


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:


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 ?



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.

lokeshshettyk
12-15-2008, 07:00 AM
Yeah, Dot should be fine :)

Fou-Lu
12-15-2008, 07:02 AM
Thanks
so using this ...


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:


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 ?



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*.


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.

jeddi
12-15-2008, 10:23 AM
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 : ?


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

any thoughts ?

Fou-Lu
12-15-2008, 10:40 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum