douhui4831 2012-02-09 10:57
浏览 28
已采纳

在php中从textarea拆分url

I don't have much knowledge about regular expression. I found this code to detect link or you can say to insert link that is written in textarea

$url = 'this is just a link http://stackoverflow.com/ to test';
$text = preg_replace("
  #((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie",
  "'<a href=\"$1\" target=\"_blank\">$3</a>$4'",
  $url
);
echo $text;

output: this is just a link stackoverflow.com to test

But i need to separate the link into a variable from that text area. For example variable $var = http://stackoverflow.com so that i can work with the link.

  • 写回答

3条回答 默认 最新

  • duanlvxi8652 2012-02-09 11:03
    关注

    You can try in this way:

    $text = 'this is just a link http://stackoverflow.com/ to test';
    $regex = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    if(preg_match($regex, $text, $url)) { // $url is now the array of urls
      // here you should check $url content and length
      echo $url[0]; // the first one in this case
    }
    else {
      // no urls found
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化