dongqingchan2385 2016-07-07 14:24
浏览 126

php使用preg_replace删除顶级域名

i want to remove certain tld domain on my string, but i got a few problem.

$MyString = 'Amazon.com: pada port .com3 Dog Name Tags.com googl.com™ any.com™ on mys.co.uk® end.Commando is .commitment.com & communism.ca';
echo preg_replace('~\.(com|info|us|org|top|xyz|co\.in|co\.uk|ca|mobi)(?:[^a-z\d]|$)~i', '', $MyString );`

the code output is

Amazon pada port .com3 Dog Name Tagsgoogltrade; any on mys end.Commando is .commitment& communism

output that i want is

Amazon: pada port .com3 Dog Name Tags googl® any™ on mys®  end.Commando is .commitment & communism
  • 写回答

1条回答 默认 最新

  • dongta5747 2017-03-16 09:37
    关注

    The issue is common: you used a consuming non-capturing group (?:[^a-z\d]|$) that actually eats up a char other than an alphanumeric or end of string and it is thus missing in the preg_replace output.

    Use a word boundary instead:

    $MyString = 'Amazon.com: pada port .com3 Dog Name Tags.com googl.com® any.com™ on mys.co.uk® end.Commando is .commitment.com & communism.ca';
    echo preg_replace('~\.(com|info|us|org|top|xyz|co\.in|co\.uk|ca|mobi)\b~i', '', $MyString );
    

    See a PHP online demo

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法