如何通过php正则表达式获取data-cfemail标签内的字符串?

I want to get 344747585151010c745359555d581a575b59 from the string.

[a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="344747585151010c745359555d581a575b59"]

I'm tried the following PHP to capture it:

preg_match_all("/<a [^>]*data-cfemail=\"?([^\">]+)\"?>/", $input_lines, $output_array);

You don't have less than or greater than symbols in your string. Try:

data-cfemail="?([^"\]]+)(?:"|])

Demo: https://regex101.com/r/JoImnS/2/

Note, this regex is looser than it could be. I assumed the double quotes encapsulating the data-cfemail were optional, if not this can be simplified.

You can make use of a positive look-behind on the data-cfemail, and then simply match any digit or lowercase letter that comes directly after that:

/(?<=data-cfemail=")[\da-z]+/

Breaking this down:

  • (?<=data-cfemail=") - match anything that follows data-cfemail="
  • [\da-z] - match any digit or lowercase letter
  • + - match one or more of the digits / lowercase letters

preg_match("/(?<=data-cfemail=")[\da-z]+/", $input_line, $output_array);

Returns:

array(1
  0 => 344747585151010c745359555d581a575b59
)

This can be seen working on PHPLiveRegex here.