Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

HOe to remove chars with php function?

Posted on 2011-09-28
12
Medium Priority
?
250 Views
Last Modified: 2012-05-12
Im tring to extract the email from the next strin
I tried using str_replace but it didnt helped for "<" " >"

From: <email@name.com>
0
Comment
Question by:Nura111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 8

Expert Comment

by:PranjalShah
ID: 36729498
Are you trying to extract just email or email@name.com?
0
 

Author Comment

by:Nura111
ID: 36734269
email@name.com
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36736496
You can use a regular expression with groups to get the information around the @ sign.  I'll show you an example in a moment.
0
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36736901
http://www.laprbass.com/RAY_temp_nura111.php
<?php // RAY_temp_nura111.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$st1 = 'From: <email@name.com>';
$st2 = 'From: email@name.com';

$rgx
= '#'               // REGEX DELIMITER
. '\b'              // WORD BOUNDARY
. '([^ ]*?)'        // GROUP OF NON-BLANK CHARACTERS
. '([@]){1}'        // GROUP OF EXACTLY ONE AT-SIGN
. '(.*?)'           // GROUP OF ANYTHING
. '([.]){1}'        // GROUP OF EXACTLY ONE DOT
. '([^ ]){2,6}'     // GROUP OF NON-BLANK CHARACTERS (THE TLD)
. '\b'              // THE WORD BOUNDARY
. '#'               // REGEX DELIMITER
. 'i'               // CASE-INSENSITIVE
;

// FIND THE EMAIL
$new = $st1;
$new = str_replace('<', NULL, $new);
$new = str_replace('>', NULL, $new);
var_dump($new);
preg_match($rgx, $new, $mat);

// SHOW THE WORK PRODUCT
var_dump($rgx);
var_dump($mat);

// FIND THE EMAIL
$new = $st2;
$new = str_replace('<', NULL, $new);
$new = str_replace('>', NULL, $new);
var_dump($new);
preg_match($rgx, $new, $mat);

// SHOW THE WORK PRODUCT
var_dump($rgx);
var_dump($mat);

Open in new window

0
 
LVL 8

Expert Comment

by:PranjalShah
ID: 36737516
This worked for me..
$remove = array("<",">");
$replace = array("","");
$string = "<email@name.com>";
echo str_replace($remove,$replace,$string);

Open in new window

0
 
LVL 8

Accepted Solution

by:
PranjalShah earned 2000 total points
ID: 36739316
If FROM: is also a part of the string,

$remove = array("FROM:","<",">");
$replace = array("","","");
$string = "FROM:<email@name.com>";
echo str_replace($remove,$replace,$string);

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36765670
Sorry... Even though the other REGEX worked, this is a better solution.  It puts the string length counts inside the parenthesized groups.
<?php // RAY_temp_nura111.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$st1 = 'From: <email@name.com>';
$st2 = 'From: email@name.com';

$rgx
= '#'               // REGEX DELIMITER
. '\b'              // WORD BOUNDARY
. '([^ ]*?)'        // GROUP OF NON-BLANK CHARACTERS
. '([@]{1})'        // GROUP OF EXACTLY ONE AT-SIGN
. '(.*?)'           // GROUP OF ANYTHING
. '([.]{1})'        // GROUP OF EXACTLY ONE DOT
. '([^ ]{2,6})'     // GROUP OF NON-BLANK CHARACTERS (THE TLD)
. '\b'              // THE WORD BOUNDARY
. '#'               // REGEX DELIMITER
. 'i'               // CASE-INSENSITIVE
;

// FIND THE EMAIL
$new = $st1;
$new = str_replace('<', NULL, $new);
$new = str_replace('>', NULL, $new);
var_dump($new);
preg_match($rgx, $new, $mat);

// SHOW THE WORK PRODUCT
var_dump($rgx);
var_dump($mat);

// FIND THE EMAIL
$new = $st2;
$new = str_replace('<', NULL, $new);
$new = str_replace('>', NULL, $new);
var_dump($new);
preg_match($rgx, $new, $mat);

// SHOW THE WORK PRODUCT
var_dump($rgx);
var_dump($mat);

Open in new window

0
 
LVL 8

Expert Comment

by:rationalboss
ID: 36813916
or simply:

<?php
$text = "hello my email is this.is@my.email.com. check out my other email too at other@email.net. email me at <hello.world@manyworlds.org>";
$pattern="/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/i"; 
preg_match_all($pattern, $text, $matches); 
echo implode('<br />',$matches[0]);
?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36813992
This seems to generate a false positive, accepting an email address for a TLD that does not exist (the longest TLD is "museum").

But that aside, you might find this article interesting.  It shows a similar issue - grabbing domain names - and shows the process that a programmer might use to develop the regular expression, with iterative development and testing.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Best of luck with your project, ~Ray
<?php // RAY_temp_rationalboss.php
error_reporting(E_ALL);

// THE TEST CASE
$text = "hello my email is this.is@my.email.com. check out my other email too at other@email.net. email me at <hello.world@manyworlds.org>";

// AN ADDENDUM TO THE TEST CASE
$text .= '<a href="mailto:ray@gmail.com">email me</a> but not with a bogus@emailaddress.fladderap';

$pattern="/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/i";
preg_match_all($pattern, $text, $matches);
echo implode('<br />',$matches[0]);

Open in new window

0
 

Author Comment

by:Nura111
ID: 36905215
I'm already working with a valid email address but its inside the format I mentioned PranjalShah:

its not working and dont I need to remove the from also?
0
 
LVL 8

Expert Comment

by:PranjalShah
ID: 36905239
I also posted one if FROM: is part of the string. The code snippet I posted worked for me. Post the code you are trying.
0
 

Author Comment

by:Nura111
ID: 36905360
ok got it its was a problem because the string was changing by htmlspecailCars before so it didnt work
0

Featured Post

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question