Extract URLs from text in PHP

后端 未结 14 2087
野趣味
野趣味 2020-11-22 13:29

I have this text:

$string = \"this is my friend\'s website http://example.com I think it is coll\";

How can I extract the link into another

相关标签:
14条回答
  • 2020-11-22 13:55
    <?php
    preg_match_all('/(href|src)[\s]?=[\s\"\']?+(.*?)[\s\"\']+.*?/', $webpage_content, $link_extracted);
    

    preview

    0 讨论(0)
  • 2020-11-22 13:56

    I tried to do as Nobu said, using Wordpress, but to much dependencies to other WordPress functions I instead opted to use Nobu's regular expression for preg_match_all() and turned it into a function, using preg_replace_callback(); a function which now replaces all links in a text with clickable links. It uses anonymous functions so you'll need PHP 5.3 or you may rewrite the code to use an ordinary function instead.

    <?php 
    
    /**
     * Make clickable links from URLs in text.
     */
    
    function make_clickable($text) {
        $regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
        return preg_replace_callback($regex, function ($matches) {
            return "<a href=\'{$matches[0]}\'>{$matches[0]}</a>";
        }, $text);
    }
    
    0 讨论(0)
  • 2020-11-22 13:56

    The code that worked for me (especially if you have several links in your $string) is:

    $string = "this is my friend's website https://www.example.com I think it is cool, but this one is cooler https://www.stackoverflow.com :)";
    $regex = '/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
    preg_match_all($regex, $string, $matches);
    $urls = $matches[0];
    // go over all links
    foreach($urls as $url) 
    {
        echo $url.'<br />';
    }
    

    Hope that helps others as well.

    0 讨论(0)
  • 2020-11-22 13:56
    preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\']+".
                    "(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/",
                    $var, &$matches);
    
    $matches = $matches[1];
    $list = array();
    
    foreach($matches as $var)
    {    
        print($var."<br>");
    }
    
    0 讨论(0)
  • 2020-11-22 13:58

    You could try this to find the link and revise the link (add the href link).

    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    
    // The Text you want to filter for urls
    $text = "The text you want to filter goes here. http://example.com";
    
    if(preg_match($reg_exUrl, $text, $url)) {
    
           echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);
    
    } else {
    
           echo "No url in the text";
    
    }
    

    refer here: http://php.net/manual/en/function.preg-match.php

    0 讨论(0)
  • 2020-11-22 13:59

    URLs have a quite complex definition — you must decide what you want to capture first. A simple example capturing anything starting with http:// and https:// could be:

    preg_match_all('!https?://\S+!', $string, $matches);
    $all_urls = $matches[0];
    

    Note that this is very basic and could capture invalid URLs. I would recommend catching up on POSIX and PHP regular expressions for more complex things.

    0 讨论(0)
提交回复
热议问题