SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist lance_vincent's Avatar
    Join Date
    Aug 2004
    Location
    philippines
    Posts
    574
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tricky STR_REPLACE

    Lets say you need to display a text teaser of the actual value, but you want to have all email format to be in "*******", say

    Code:
    $var = "Please email me at lance@workemail.com or at my private email lance@yahoo.co.uk";
    should display as
    Code:
    Please email me at ******************* or at my private email ******************
    How would you accomplish this? Is there a way to use str_replace to match a particular regular expression then replace that with a fix ten character asterisk? I wanted to hear your opinions
    If you won't dress like the
    Victoria Secret girls,
    don't expect us to act like soap opera guys.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,891
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    preg_match_all('/[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))/'$var$results);
    foreach(
    $results[0] as $email)
    {
    $var str_replace($emailstr_repeat('*'strlen($email));

    Something like that?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for regular expression you can use: preg_replace

  4. #4
    SitePoint Evangelist lance_vincent's Avatar
    Join Date
    Aug 2004
    Location
    philippines
    Posts
    574
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    PHP Code:
    preg_match_all('/[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))/'$var$results);
    foreach(
    $results[0] as $email)
    {
    $var str_replace($emailstr_repeat('*'strlen($email));

    Something like that?
    Thanks! That was fast, you didn't make it look like it was tricky in any sense.

    Follow up question since im not much of a preg_match user, the reg expression you provided is rather lengthy than the usual, can u explain the part where there is "aero|coop|info|museum|name"?

    one more thing, you missed an additional parameter on the str_replace line
    If you won't dress like the
    Victoria Secret girls,
    don't expect us to act like soap opera guys.

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's probably overly lengthy in a lot of respects, but it depends on your content.
    With regards to "aero|coop|info|museum|name", it is in addition to "[a-zA-Z]{2,3}" which checks for things like .us or .com, but not, say, 6 letter TLD like museum.

    More info here http://www.regular-expressions.info/email.html

  6. #6
    SitePoint Evangelist lance_vincent's Avatar
    Join Date
    Aug 2004
    Location
    philippines
    Posts
    574
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    That's probably overly lengthy in a lot of respects, but it depends on your content.
    With regards to "aero|coop|info|museum|name", it is in addition to "[a-zA-Z]{2,3}" which checks for things like .us or .com, but not, say, 6 letter TLD like museum.

    More info here http://www.regular-expressions.info/email.html
    Thanks hash, ill check on the link to read further. I hope to work on string manipulations more so i can learn these things while working.
    If you won't dress like the
    Victoria Secret girls,
    don't expect us to act like soap opera guys.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •