/    Sign up×
Community /Pin to ProfileBookmark

stripping out @ from a string ?

Hi all,
does anyone know I strip out
the @ and . (period) from an email address ?

I want my string say [email][email protected][/email]

to become johnhettlecom

I’d like to do this in php not javascript if possible.

Thanks for your help.
David ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsApr 20.2005 — try[code=php]<?php
$str = '[email protected]';
echo preg_replace(array("/@/", "/./"), array('', ''), $str);
?>[/code]
Copy linkTweet thisAlerts:
@leleutdApr 20.2005 — While you could do a regular expression replacement, I take it to mean that you would like to extract the username and domain from the string. Here's how you would do that:

<?php

$email = "[email protected]";

$at_location = strpos($email, "@");

$dot_location = strrpos($email, ".");

$len = strlen($email);

$username = substr($email, 0, $at_location);
$domain = substr($email, $at_location + 1, $dot_location - $at_location - 1);
$tld = substr($email, $dot_location + 1, $len - $dot_location);

echo "User: $usernametDomain: $domaintTLD: $tldn";

?>

HTH,

Travis
Copy linkTweet thisAlerts:
@JonaApr 20.2005 — [font=Trebuchet MS]Why use a regular expression? If you include the regular expression overhead engine, you increase the processing time of your page. Optimally, you would use the [/font][font=courier new]str_replace()[/font][font=trebuchet MS] function.[/font]

[code=php]
<?php
$str = '[email protected]';
$str = str_replace (array('@','.'), array('',''), $str);
?>
[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsApr 21.2005 — [font=Trebuchet MS]Why use a regular expression? If you include the regular expression overhead engine, you increase the processing time of your page. Optimally, you would use the [/font][font=courier new]str_replace()[/font][font=trebuchet MS] function.[/font]

[code=php]
<?php
$str = '[email protected]';
$str = str_replace (array('@','.'), array('',''), $str);
?>
[/code]
[/QUOTE]


Or even:
[code=php]
<?php
$str = '[email protected]';
$str = str_replace (array('@','.'), '', $str);
?>
[/code]
Copy linkTweet thisAlerts:
@webgovernorApr 21.2005 — Well, if it's any help, I vote

for either preg_replace() or str_replace()...
Copy linkTweet thisAlerts:
@JonaApr 21.2005 — [font=trebuchet ms]Thanks. Crossed my mind, but I didn't test it, so I just used what I know works.[/font]
×

Success!

Help @DaveinLondon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...