Avatar of melwong
melwong asked on

how to detect less than (<)character in php ?

hi everybody ,i form malaysia, i have meet some problem about php code.
i would like to extract <example.com
but nothing  result come out...
Do your have any solution for this problem or any suggestion to soft this porblem?

Thank you  
<?php
$email  = 'name<example.com';
$domain = strstr($email,'<');
echo $domain; // prints @example.com
 
?>

Open in new window

PHPRegular Expressions

Avatar of undefined
Last Comment
AlexanderR

8/22/2022 - Mon
F. Dominicus

I do not get what you say, I took you source run it through php5
and got
 php5 t6.php
<example.com%                
So exactly what I expected.
Maybe you better write
<?php
$email  = 'name<example.com';
$domain = strstr($email,'<');
echo "$domain\n";
 
?>

to really see the output
But the code is just fine

Regards
Friedrich
waygood

mine prints '<example.com' which is correct.
AlexanderR

He said:
echo $domain; // prints @example.com

perhaps he wants to extract it AND replace with @ sign??

$email  = 'name<example.com';
$domain = strstr($email,'<');
$domain = substr_replace($domain, '@', 0, 1);
echo $domain; // prints @example.com

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
F. Dominicus

Well yes if he want that, that would be one way to go.

Regards
Friedrich
ASKER
melwong

Sorry guys,

I think i better rephrase myself,

-I have a string such as $email = "Jeff <jeff@yahoo.com>";
-I wish to extract only jeff@yahoo.com, without the "<" and ">"

How do i do that cos the code that i attached couldn't read the "<".

ASKER CERTIFIED SOLUTION
F. Dominicus

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
melwong


hi fridom,

i tried to attach your code into my code  , but cannot work , what are the problem for this coding?

Thank

<?php
 require ('pop3.class.inc'); 
 
$pop3 = new POP3; 
 
// Connect to mail server 
$do = $pop3->connect ('mail.justmobile.com.my'); 
if ($do == false) { 
    die($pop3->error); 
}
 
// Login to your inbox 
$do = $pop3->login ('jeffrey@justmobile.com.my', 'jmsb1288'); 
 
if ($do == false) { 
    die($pop3->error); 
} 
 
// Get office status 
$status = $pop3->get_office_status(); 
 
if ($status == false) { 
    die($pop3->error); 
} 
 
$count = $status['count_mails']; 
 
if ($count == '0') { 
    echo 'There are no new e-mails'; 
} 
 
for ($i = 1; $i <= $count; $i++) { 
    $email = $pop3->get_mail($i); 
 
    if ($email == false) { 
        echo $pop3->error; 
        continue; 
    } 
 
    $email = parse_email ($email); 
	
	$subject = htmlentities($email['headers']['Subject']);
	
	$subject = trim($subject);
	
	$subject = strtolower($subject);
	
	if (strcmp($subject, "unsubscribe") == 0) {
		
		$s_string =htmlentities($email['headers']['From']);
		
	    $regexp = "[^<]<((.*)+)>";
		
		$found = ereg($regexp, $s_string, $found_arr);
		
	if ($found){
			echo "Found something, $found_arr[1]\n";
 } 	else {
			echo "Did not found anything";
 }
 
		unsubscribe(htmlentities($email['headers']['From']));
 
	}
 
    // Remove from mail server 
#    $do = $pop3->delete_mail ($i); 
#    if ($do == false) { 
#        echo $pop3->error; 
#    } 
} 
 
$pop3->close(); 
 
function parse_email ($email) { 
    // Split header and message 
    $header = array(); 
    $message = array(); 
 
    $is_header = true; 
    foreach ($email as $line) { 
        if ($line == '<HEADER> ' . "\r\n") continue; 
        if ($line == '<MESSAGE> ' . "\r\n") continue; 
        if ($line == '</MESSAGE> ' . "\r\n") continue; 
        if ($line == '</HEADER> ' . "\r\n") { $is_header = false; continue; } 
 
        if ($is_header == true) { 
            $header[] = $line; 
        } else { 
            $message[] = $line; 
        } 
    } 
 
    // Parse headers 
    $headers = array(); 
    foreach ($header as $line) { 
        $colon_pos = strpos($line, ':'); 
        $space_pos = strpos($line, ' '); 
 
        if ($colon_pos === false OR $space_pos < $colon_pos) { 
            // attach to previous 
            $previous .= "\r\n" . $line; 
            continue; 
        } 
 
        // Get key 
        $key = substr($line, 0, $colon_pos); 
 
        // Get value 
        $value = substr($line, $colon_pos+2); 
        $headers[$key] = $value; 
 
        $previous =& $headers[$key]; 
    } 
 
    // Parse message 
    $message = implode('', $message); 
 
    // Return array 
    $email = array(); 
    $email['message'] = $message; 
    $email['headers'] = $headers; 
 
    return $email; 
} 
 
 
 
function unsubscribe($email)
{
 
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("frenszone", $con);
 
$result = mysql_query("UPDATE frenszone_recipients_list SET unsubscribe=1 WHERE recipient_email=$email");
 
mysql_close($con);
 
}
 
 
 
 
 
?> 

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
F. Dominicus

I ask you kindly to change the question over and over again. We answered your first question. I answered you second question up to the point. NOW you ask us to answer a third question.

I for my part do not feel that is "fair". So I won't look into it.

Regards
Friedrich
ASKER
melwong

Sorry for causing confusion, the question i ask actually are the same,just that i put in different words.I tried your solution but it did't work.That why i attached my original code where i implemented your solution,as shown below.Care to look into it? For your effort i increased the points to 350.


if (strcmp($subject, "unsubscribe") == 0) {
               
                $s_string =htmlentities($email['headers']['From']);
               
            $regexp = "[^<]<((.*)+)>";
               
                $found = ereg($regexp, $s_string, $found_arr);
               
        if ($found){
                        echo "Found something, $found_arr[1]\n";
 }      else {
                        echo "Did not found anything";
 }
 
                unsubscribe(htmlentities($email['headers']['From']));
 
        }
F. Dominicus

How should I know what's in s_string? I posted the code you see which variable was assigned and you have seen the output. So have you checked my code or not? What was the outcome? In you code I can not see what subject contains etc. I've not the slightes idea on waht's in $email etc. Howerver if my code works as I said it did then you got the email address without the braces. If you don't get it your $s_string is not the one posted.

Friedrich
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
AlexanderR

"How should I know what's in s_string?..In you code I can not see what subject contains etc. I've not the slightes idea on waht's in $email"

Actually, you can.
There is a $pop3 variable that is an instance of a class POP3.  It has many methods, connect, status and so on.  The one in question is the method get_mail.  Through the way that method is used you can see that it returns an associative multi-dimensional array with email details, all of which are later kept in $email.  The one in question is the sender member: $email['headers']['From'] which, according to the original message has a format "real name <email@domain.com>" (gmail address book has a similar address book format, for example).

Overall, this looks like some kind of un-subscription scripts.  Users send emails to a particular address, then this script gets all the address from the inbox and  takes them out of subscription database (or in this case sets them to unsubscribe =1 ) so that they do not get any more newsletters or whatever.

His problem, is that the database stores emails as  "email@domain.com" while the email server returns "real name <email@domain.com>".  So he needs a script that extracts the email portion and passes it to the database.

I would like for melwong to confirm this logic first, please, before going further.
F. Dominicus

No we don't know what's in $email, because it was not posted. So we should craft an regular expression without knowing what may or may nob be there. And BTW the original question was changed at least two times.
and if you had read the posted regular expression you would have seen that
$regexp = "[^<]<((.*)+)>";
that reads ignore everything not beeing a < then match a < then some arbitrary number (at least one of anything)
then a > 

So fine even if the server returned
real name <email@domain.com>
it found this as you easily can see

php5 t7.php
Found something, jeff@yahoo.com
So what?

Friedrich

AlexanderR

Sorry, regex is not my strong point so i did not fully appreciate the meaning of [^<]<((.*)+)>.

Your code should be perfect.  Its then a matter of how he implements it.  If i continue to use my guessed logic, then i think his code is out to look something like this.
for ($i = 1; $i <= $count; $i++) { 
    $email = $pop3->get_mail($i); 
 
    if ($email == false) { 
        echo $pop3->error; 
        continue; 
    } 
 
    $email = parse_email ($email);  
    $subject = htmlentities($email['headers']['Subject']);
    $subject = trim($subject);
    $subject = strtolower($subject);
    if (strcmp($subject, "unsubscribe") == 0) {
      $s_string =htmlentities($email['headers']['From']);
      $regexp = "[^<]<((.*)+)>";
      $found = ereg($regexp, $s_string, $found_arr);
      if ($found){
           unsubscribe(htmlentities($found_arr[1]));
      } else {
           echo "Error: email server returned unexpected email address format";
      }
}

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
melwong

hi fridom,
sorry for late reply your message,i had tested your code .Its worked properly,but when i tried to put inside my code ,then got error.I apologized for my mistake fridom.

Alexandra gets what i mean,thank for the explanation.
F. Dominicus

Well than close this question please
Regards
Friedrich
AlexanderR

No problem :)

What do you get when you use my last suggestion?
Your help has saved me hundreds of hours of internet surfing.
fblack61