Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6157315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:45:51+00:00 2026-05-23T20:45:51+00:00

I am using a regex to replace all email addresses in a string with

  • 0

I am using a regex to replace all email addresses in a string with a nice <a> to make them clickable. This works perfect, except for the case when there are two words of a certain minimum length and a dash between them in front of the email address. Only then I get an empty string as result.

<?php

$search = '#(^|[ \n\r\t])(([a-z0-9\-_]+(\.?))+@([a-z0-9\-]+(\.?))+[a-z]{2,5})#si';
$replace = '\\1<a href="mailto:\\2">\\2</a>';

$string = "tttteeee-sssstttt mail@test.nl";
echo preg_replace($search, $replace, $string);
// Output: "" (empty)

$string = "te-st mail@test.nl";
echo preg_replace($search, $replace, $string);
// Output: "te-st <a href="mailto:mail@test.nl">mail@test.nl</a>" (as expected)

$string = "mail@test.nl tttteeee-sssstttt";
echo preg_replace($search, $replace, $string);
// Output: "<a href="mailto:mail@test.nl">mail@test.nl</a> tttteeee-sssstttt" (as expected)

?>

I have tried everything, but I really can’t find the problem. A solution would be removing the first dash in the regex (before the @ sign), but that way email addresses with a dash before the @ wouldn’t be highlighted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:45:52+00:00Added an answer on May 23, 2026 at 8:45 pm

    OK, minimum use case: #([a-z-]+\.?)+@#, which reaches the backtrack limit (use preg_last_error()), it cannot determine where to put things, as the \. is optional, determining whether to use the inside or the outside + is a lot of work. The default limit of pcre.backtrack_limit of 100000 does not work, setting it to 1000000 does.

    To solve this, make it easier on the parser: the first (([a-z0-9\-_]+(\.?))+ should become: ([a-z0-9\-_]+(\.[a-z0-9\-_]+)*), which is a lot easier to solve internally. And as a bonus, instead of the accepted answer, this still doesn’t allow consecutive dots.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using c# regex.replace and I want to replace this string: test =
I have been using the following regex to replace all punctuation in a string:
I'm currently modifying my regex for this: Extracting email addresses in an html block
i'm having a string var str = hello -- (world); using regex and replace
First of all, I know that using regex for email is not recommended but
I'm using the regex System.Text.RegularExpressions.Regex.Replace(stringToSplit, ([A-Z]), $1).Trim() to split strings by capital letter, for
I am using a simple regex to replace break tags with newlines: br_regex =
im new with regexp, so can i ask for some assistance Using string.replace function
I am trying to replace all the #include whatever.h with #include <whatever.h> using find
I've found loads of examples on to to replace text in files using regex.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.