zero-perfoliate
zero-perfoliate

Author Topic: If variable blank display nothing  (Read 194 times)

Offline travist6983

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
If variable blank display nothing
« on: July 26, 2010, 07:44:45 AM »
OK guys thanks for reading my post!

i am having a slight issue with a contact form and that is if someone doesnt fill in the phone number box then it just ads a blank space to the email that gets sent such as below:

This is filled out -
Name: Larry Boy
Phone: 703-922-4010
Email: larry@skbk.com

This is not filled out-
Name: Larry Boy

Email: larry@skbk.com

I am using this to figure out if it is actually blank or not:

Code: [Select]
if ($Tel == "")
$TelResults = "";
else
$TelResults = "Phone: ".$Tel;

and this to display it on the email
Code: [Select]
$Body .= $TelResults;

I understand this is VERY small thing but my boss hates getting contacts forms that "dont look right" i have scowered the net trying to figure this out so now i am turning to this forum for some help... Can someone please point me in the right directions...


Offline collegemike

  • PHP Workers
  • **
  • Posts: 5
  • Karma: +0/-0
Re: If variable blank display nothing
« Reply #1 on: August 06, 2010, 09:42:50 PM »
Larry,

You might try doing a regular expression to search for an email. Something like:

Code: [Select]
<?php

$form "This is filled out -
Name: Larry Boy
Phone: 703-922-4010
Email: larry@skbk.com

This is not filled out-
Name: Larry Boy"
;
$regex "/\w*\@\w*\.[[:alpha:]]{2,3}/i";

echo preg_match($regex$form);
?>


This will print how many times an email address consisting of some_letters_or_numbers@somemore_letters_or_numbers.2_or_3_letters appears... in this case there is one match (larry@skbk.com)

Breakdown of regular expression pattern:
/ expression /i - denotes that string is a regular expression (trailing "i" means match any case)
\w - match any alphanumeric (A-Z or 0-9) character or underscore (_)
* - look for zero or more of this expression. For example \w* means look for zero or more alphanumeric characters
[[:alpha:]] - look only for alphabetic characters (no numbers)
{2,3} - look for 2 or 3 of this expression. For example [[:alpha:]]{2,3} means look for between 2 or 3 letters

If you need more help with regular expressions I'd check out http://php.net/manual/en/function.preg-match.php

-Mike

Offline collegemike

  • PHP Workers
  • **
  • Posts: 5
  • Karma: +0/-0
Re: If variable blank display nothing
« Reply #2 on: August 06, 2010, 09:50:31 PM »
Larry,

Just noticed I slightly misunderstood what you were asking. You can design a regular expression to match the telephone number instead, which will tell you if there exists a valid phone number in the email.

-Mike