A few simple PHP questions...

Associate
Joined
29 Dec 2004
Posts
2,253
Hi,

I'm trying to do a few basic things in PHP, problem being i am used to tweaking other's code rather than writing it from scratch so im having a few problems. The first thing i'd like to do is create a statement saying IF foo then echo bar, the problem being that the string containing foo also has a lot of other bits in it that id like to ignore. Here's an example, i have a combobox with lots of addresses, like so:

AAA - Mr Test, Test Town, Testshire, AA11 1AA
AAA - Another Test, No Reason, Just Felt Like It, BB1 1AB
BBB - Test again, But this time with a different beginning, AA12 3DB

What i'd like to do is say IF any part of that string matches AAA then echo "Some statement about AAA", or IF any part of that string matches BBB then echo "A different statement about BBB". I have about 8 different "zones" and this is what these "AAA" refer to.

So basically, how do i code that in PHP? I know how to do a basic IF statement, just not how to search for a certain bit of text in a string? There's more to come but this is my starting block.

TIA
 
Last edited:
look at strstr() and stristr() depending on whether you want case sensitive string searching. That's assuming you want to search for AAA in a string - anything more complicated, have a look at regular expressions and preg_match()
 
Sounds like a job suited for regular expressions... Do a search on the PHP function preg_match, I'm admittedly very, very bad at regular expressions, but I think that'll help you on your way. :p
 
strpos() is more efficient than strstr()

Code:
if (strpos($string, 'AAA') !== false) {
  echo 'Something about AAA';
} else if (strpost($string, 'BBB') !== false) {
  echo 'Something about BBB';
} //etc
or:
Code:
switch (true) {
  case (strpos($string, 'AAA') !== false): 
    echo 'Something about AAA';
    break;

  case (strpos($string, 'BBB') !== false):
    echo 'Something about BBB';
    break;

  //etc
}
 
Well i'm making progress, but am stuck on one bit. The following works perfectly fine if the string ($theaddress) contains the first part of the IF statement, but if its anything else i get a blank white page. Here's my code:

Code:
$theaddress = $_POST['address'];

if (strpos($theaddress, 'EBD') !== false) {
  $subto = 'Somebody here';
} else if (strpost($theaddress, 'HR') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'BH') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'AAW') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'AAR') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'HC') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'MSX') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'WSX') !== false) {
  $subto = 'Somebody else here';
}

For example, if $theaddress contains EBD it works perfectly, but if it contains anything else (either one of the above conditions such as AAW, MSX, WSX, etc, or none of those conditions) it will fall over and display a white page. I'm sure it's something simple and appreciate any help :p

Oh and i also tried it with the break; as above but no luck.
 
Last edited:
Ok, so here's my next problem. I am doing all of this to basically allow our team to submit orders to our procurement lady a lot quicker, one requirement is that the output can be sent as an attachment (either plain text or HTML file) to an email address.

So next question is, is that possible - can i use PHP to take the output from a form, place it into a templated HTML file, and then send it "attached" to an email address rather than being in the body of the email?
 
http://pear.php.net/package/Mail

http://pear.php.net/package/Mail_Mime

I then use the following class as a wrapper to send emails and attachments

Code:
<?php
	#pear classes are used because they're awesome
	require_once('Mail.php');
	require_once('Mail/mime.php');
	
	class Email {
		private $mime; # holds the Mail_mime object
		private $mail; # holds the pear mail factory object
		
		public $Recipients = array();
		private $From;
		private $Subject;
		
		private $Headers = array();
		private $MBody;
		
		public function __construct() {
			$this->mime = &new Mail_mime;
			$this->mail =& Mail::factory('mail');
		}
		
		public function AddRecipient($email) {
			array_push($this->Recipients,$email);
		}
		
		public function SetFrom($name,$email) {
			$this->From = sprintf('%s <%s>',$name,$email);
		}
		
		public function SetSubject($subject) {
			$this->Subject = $subject;
		}
		
		public function RemoveRecipient($email) {
			$k = array_search($email,$this->Recipients);
			if ($k !== false && array_key_exists($k,$this->Recipients)) {
				unset($this->Recipients[$k]);
				$this->Recipients = array_values($this->Recipients);
				return true;
			}
			return false;
		}
		
		public function SetHTML($body) {
			$this->mime->setHTMLBody($body);
		}
		
		public function SetText($body) {
			$this->mime->setTXTBody($body);
		}
		
		public function AttachFile($filename,$type = 'application/octet-stream') {
			$this->mime->addAttachment($filename,$type);
		}
		
		private function Prepare() {
			$this->MBody = $this->mime->get();
			if (trim($this->From) == '' || trim($this->Subject) == '') {
				return false;
			}
			$this->Headers['From'] = $this->From;
			$this->Headers['Subject'] = $this->Subject;
			
			$this->Headers = $this->mime->headers($this->Headers);
			return true;
		}
		
		public function Send() {
			if (false === $this->Prepare()) {
				return false;
			}
			foreach ($this->Recipients as $r) {
				$this->mail->send($r,$this->Headers,$this->MBody);
			}
		}
	}
?>
 
Awesome, thanks for that!

One more "main" question:
Are there any utilities that will allow me to "mail merge" the output of a form into a word processor templated document? For example, our current order form is a Word template and our procurement lady would like to keep a similar format...so i was hoping i could "merge" the form output with that template?

Thanks again.
 
Ok another question about getting info out of a string. Same situation as above, an example of what i have in a combobox:

AAA - Dr Test, Test Town, Testshire, AA11 1AA
AAA - Dr Another , No Reason, Just Felt Like It, BB1 1AB
BBB - Dr Again, But this time with a different beginning, AA12 3DB

Let's say i wanted to extract the doctor's name, for example just to output "Dr Another". How do i go about doing that? Obviously i can't use the same thing as before?

Edit: Have been looking into preg_match to do the job but having problems starting, can anyone provide a little bit of example code for how i could use it?
 
Last edited:
I wouldn't use preg_match for that to be honest - for that sort of thing, I normally use substr, strpos and/or strrpos

Code:
 $str = substr($str,strpos($str,'-'));
 $str = substr($str,0,strpos($str,',')-1);
 $str = trim($str);

that'll return the bit of the string from the first hypen to the first comma - obviously not foolproof, but then if you're just having a free text box for people to enter information in, you're going to get quite a variety of input that will probably elude regex too.
 
Beauty! Works a charm, tweaked it around by removing the -1 from the second line and adding +1 to the first line (as it was losing the last letter of the name and showing the hyphen!) but that is excellent. Thanks very much.

Anyone know about my previous question...merging form output into a Word/Open Office document template?

Oh and the string comes from a combobox, so should all follow the same format.
 
Last edited:
Right then, this is my main problem now. Does anyone know of any PHP script that would allow me to take the output from the form and export it to either a CSV file or Word template so that it can be emailed? I was hoping to do it by putting it into an HTML template but unfortunately people want to be able to edit it after is has been submitted...

Help!!
 
CSV is easy, I don't know anything about Word docs, though. If you sent someone a text file they could open that in Word.

have a look at fopen (specifically the 'w' parameter when creating a file handler), fwrite and fclose for file creation, then just create a CSV like you would if it were treated as a plain text file; "File","Name","Address"\n
 
Am making progress, do you know how to format the string im inputting into the file so that Word will open it without wanting to convert it to something? Can i force it to be UTF-8 or something? Also, when i use \n and open it in Notepad it shows a little square symbol rather than adding a line break. Can i sort this somehow?
 
ah yes, you non-unix types! someone else will be able to tell you for sure, but does \r\n work in Windows?

UTF-8! ugh, I have had a headache with this!

try:
Code:
header('Content-type: text/html; charset=utf-8');
 
Nice one, \r\n works a treat. The UTF thing i was talking was because when i opened the .txt file in Word it wanted to convert it to some MS format...but since doing \r\n it's sorted that as well so i'm all good!

Do you know of any way to format things so it looks slightly "fancier" or is it only possible to do plain text outputs?
 
according to this you can use fopen to create a .doc file, just by naming it something.doc and then putting html in. I'd be very surprised if that works, but it's worth a try

edit: cool!! seems to work :D
 
Last edited:
Back
Top Bottom