[SOLVED] strpos and substr not giving expected results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

[SOLVED] strpos and substr not giving expected results

Post by impulse() »

I'm parsing an e-mail using a MIME parser from phpclasses.org and then inserting the data into a MySQL database. But because some of the e-mail have more than 1 delivery address the output is as follows:

"'Mr A'" <a@x.com>,
"'Mrs B'" <b@x.com>,

So far I have exploded all the data by " which has left me with lines of the following:

'A'

,

'B'

,

Then I've used the following code to go through each line and find the first ' and the 2nd ' and then use substr to put everything inbetween into another variable, using the following code.

Code: Select all

$toSplit = explode("\"", $toInsert);

foreach($toSplit as $a) {
  $start[$i] = strpos($a, "\"");
  $end[$i]   = strpos($a, "\"", $start[$i]);
  $out[$i]   = substr($a, $start[$i], $end[$i]);
  $i++;
}
But this doesn't print any data out. Strpos doesn't seem to be finding the start and end occurences of the speech marks.

Can anybody see why in this example?

Regards,
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I've used the following code:

Code: Select all

$toSplit = explode("\"", $toInsert);

        $i = 0;

        foreach($toSplit as $a) {
          $out[$i] = trim($a, "\'");
          $i++;

        }

        $toInputDB = "";
        foreach($out as $a) {
          $toInputDB .=  strip_tags($a). " ";
        }
Post Reply