我有此格式的域列表
domain1.com
domain2.net
domain3.org
domain4.info
我想在域名扩展名末尾之前用星号替换名称
*****.com
********.net
**********.org
*****.info
有可能这样做吗?
答案 0 :(得分:0)
使用正则表达式Lookahead
$re = '/(.+?)(?=\.)/m'; // OR (.+?)(?=\.[a-zA-Z]{2,11})
$str = 'domain1.com
domain2.net
domain3.org
domain4.info';
$subst = '*******';
$result = preg_replace($re, $subst, $str);
输出:
*******.com
*******.net
*******.org
*******.info
REGEX https://regex101.com/r/HPDhtA/2
按评论
<?php
$re = '/(.+?)(\.[a-zA-Z]{2,11})/m';
$str = 'domain1.com
domain2ddddddddd.net
domain3dfdfdf.org
domain4.info';
$result = [];
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach($matches as $match){
$result[] = str_repeat("*",strlen($match[1])).$match[2];
}
// Print the entire match result
echo implode(PHP_EOL,$result);
输出:
*******.com
****************.net
*************.org
*******.info