dppcyt6157 2014-02-05 09:48
浏览 22
已采纳

从字符串中获取主题标签,替换其中的一些符号并将其转换为链接PHP

I have some string with hashtags looks like @user_name. Now i convert all hashtags to links this way:

$text = preg_replace ("/@(\\w+)/", '<a href="http://$1.'.SITE_NAME.'">@$1</a> ', $text);

As you can see, all hashtags becomes to subdomain names. If you know, there some problems with _ symbol in subdomain names (some browsers not supports that, IE supports, but not set cookies, etc..). So i need to replace the _ symbols in subdomain to - (minus), but keep _ symbols in hashtag view. There link what i need to <a href="http://user-name.site.com">@user_name</a>. How?

  • 写回答

2条回答 默认 最新

  • dongyun6229 2014-02-05 10:11
    关注

    You can use preg_replace_callback () like this:

    $text = preg_replace_callback ("/@(\\w+)/", function ($matches) {
        return '<a href="http://'. str_replace('_', '-', $matches[1]) .'.abc.com.">'.$matches[1].'</a> '; },  "test @user_john here");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程