hey, I have the following preg_replace function found on php.net

$str = "Email me: [email]matt@blahblah.com[/email]";
$str = preg_replace('#(.*)\@(.*)\.(.*)#','<a href="mailto:\\1@\\2.\\3">\\1@\\2.\\3</a>',$str);
echo $str;

the above code hyperlinks the whole sentance! so it looks like

<a href='mailto:Email me: matt@blahblah.com'>Email me: matt@blahblah.com</a>

any ideas what's up - I have tried a few things and they've not worked...

thanks for any info 🙂

    How about this:

    $str =  '<a href="mailto:' . trim(strrchr($str, ' ')) . '">' . $str . '</a>';
    

      thanks.... but i need it to look through a whole string of text - so the str_replace wont work?

      eg:

      This is my string and there will be www.websiteaddress.com and some mfacer@blblblb.com email addresses in there.

      I need the above to be automatically hyperlinked when got out of the DB. Ie: they are stored as plain text in the DB.

      thanks 🙂

        The expression you're using is badly crafted. First of all, the dot in the second half of the email address needs to be escaped (.) or it will mean "any character." But also .* will match any character any number of times, and will match as many as possible unless you tell it to not be greedy (by adding the U flag -- see PHP manual). Try using this expression:

        /(\S+)@(\S+).(\S+)/

        This will match:
        - at least 1 non-space-character, followed by
        - @, followed by
        - at least 1 non-space-character, followed by
        - . (dot), followed by
        - at least 1 non-space-character.

        I haven't tested it but it should match any email address, although it will also match invalid email addresses with non-ascii characters and such. Let me know what happens!

        P.S. I found this handy reference:
        http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/perlintro2_table1.html

          linus' expression should work, especially if, as your latest post implies, you expect more than one address per string and you use a function that returns an array.

          Just for interest, if you do expect only one address in the string, this will hyperlink it:

          $str = $your_latest_example;
          $addr = extract_address($str);
          echo '<a href="mailto:' . $addr . '">Email me: ' . $addr . '</a>';
          
          function extract_address($string)
          {
              $last = strstr($string, '@');
              $first = str_replace($last, '', $string);
              $drop = strstr($last, ' ');
              $domain = str_replace($drop, '', $last);
              $user = strrchr($first, ' ');
              $address = trim($user . $domain);
              return $address;
          }

          Or, a perlish-one-liner version:

          $s = $your_latest_example;
          $s='<a href="mailto:'.($a=trim(strrchr(str_replace(strstr($s,'@'),'',$s),' ').str_replace(strstr(strstr($s,'@'),' '),'',strstr($s,'@')))).'">Email me: '.$a.'</a>';

            works perfectly now, thank you... 🙂 just in case any one wants the whole code...

            $str = "Hello this is my email [email]matt@dfdfd.com[/email] and my website is [url]www.fffff.com[/url]";
            
            $str = preg_replace('/(\S+)@(\S+)\.(\S+)/','<a href="mailto:\\1@\\2.\\3">\\1@\\2.\\3</a>',$str);
            					$str = preg_replace('=([^\s]*)([url]www.[/url])([^\s]*)=','<a href="http://\\2\\3" target=\'_blank\'>[url]http://\\2\\3[/url]</a>',$str);
            
            echo $str;
            

              Hi, I think you are missing a backslash again.

              If you change the string to "Hello this is my email matt@dfdfd.com and my wwwebsite is www.fffff.com", does it match wwwebsite?

              Something else, you might find it helpful that \0 contains the complete match, could be used instead of \1@\2.\3.

                And to cut down on the number of backslashes required, the \1, \2, etc., in the replacement string can be replaced by $1, $2, etc. (PHP won't mistake them for variables because variable names can't start with a digit).

                  Write a Reply...