Jump to content

PHP substr bug when parsing an email address?


Mateo1041

Recommended Posts

Hi all,

 

Is the following a substr function PHP bug?  This is also happening with regex functions.

 

$EmailLogFull["Content"] = "mail(\"Firstname Lastname\" <[email protected]>, this is a test";
$pos1 = strpos($EmailLogFull["Content"], "mail(") + strlen("mail(");
$pos2 = strpos($EmailLogFull["Content"], ",");
$result = substr($EmailLogFull["Content"], $pos1, $pos2 - $pos1);

 

Basically, I'm looking to extract the email address from the above string.  But it keeps removing the email address portion due to what I assume is the "<" character.

 

Anyone have any ideas?

 

Thanks much,

- Matt

This is an exact copy of the code.  I also pasted this same code into a separate stand-alone PHP file and have the same problem.  I'm displaying the $result this way:

 

echo "<h3>" . $result . "</h3>";

 

All I'm getting is this with no email address:

 

"Firstname Lastname"

 

http://data.bluegoose.org/test.php

Your code works fine, but the browser is treating the email as an HTML tag because it's in <...>. You need to wrap $result with htmlspecialchars() and you should be good to go.

 

<?php echo "<h3>" . htmlspecialchars($result) . "</h3>"; ?>

The reason anything between <> must be treated is a tag is that HTML is designed to be backwards compatible.  That is, an old HTML parser must be able to ignore newer HTML tags invented later.  So if it finds a tag it doesn't know, it will assume that it's a tag from some future version of HTML, and skip it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.