[SOLVED] Strpos() not detecting less than character (<)

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() not detecting less than character (<)

Post by impulse() »

I have an array which contains the parse of an e-mail address from a .EML file. Each element looks like the following:


1) "'Namea'"<Namea@x.com>
2) "'Nameb'"<Nameb@x.com>
3) "'Namec'"<namec@entagroup.com>

I want to extract the e-mail address from each of those elements so I have the following code:

Code: Select all

foreach($toSplit as $a) {

          $b[$i]      = str_replace(" ", "", $a);
          $start[$i]  = strpos("<", $b[$i]);
          $end[$i]    = strpos(">", $b[$i]);
          $output[$i] = substr($b, $start[$i], $end[$i]);
          $i++;

        }
But the $start nor $end elements are being filled.

Any idea for this?

Regards,
Last edited by impulse() on Tue Mar 27, 2007 9:43 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you sure they are not in another form such as HTML entity?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I've checked the HTML source and it doesn't seem so. I've tried this code (which I think should work correctly):

Code: Select all

foreach($toSplit as $a) {

          $b[$i]      = str_replace(" ", "", $a);
          $start[$i]  = strpos(html_entity_decode("<", $b[$i]));
          $end[$i]    = strpos(html_entity_decode(">", $b[$i]));
          $output[$i] = substr($b[$i], $start[$i]);
          $i++;

        }
But I still have the same results.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

THis is quite embarrasing - I was using print_r to show all the data but because each element was wrapped with "<" & ">" the browser was treating them as HTML tags and not outputting the data. At some point it wasn't outputting the HTML source, but that must of been at a point where there was an error :D

I have learned about HTML entities though so it's not all bad.
Xoligy
Forum Commoner
Posts: 53
Joined: Sun Mar 04, 2007 5:35 am

Post by Xoligy »

Why not use REGEXP?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I hate RegExp, I've read a bit about it and I've never liked it from day 1. I'll only learn how to use it when it's the only way to do something. In the meantime I'm quite happy without it.

End code by the way:

Code: Select all

foreach($toSplit as $a) {

          $b[$i]       = str_replace(" ", "", $a);
          $start[$i]   = strpos($b[$i], "<");
          $end[$i]     = strpos($b[$i], ">");
          $output[$i]  =  substr($b[$i], $start[$i], $end[$i]);
          $output[$i]  = ltrim($output[$i], "<");
          $output[$i]  = rtrim($output[$i], ">");
          $i++;

        }
Post Reply