0

i am trying to detect url that includes slash (like: http://example.com/posts/30) in a string, and get it as a html tag like:

<a href="http://example.com/posts/30">http://example.com/posts/30</a>

now, i have a function that doing this, but links with slashes not work, i get back an empty link name, other links works perfect (like: http://example.com/page.php?id=1), this is my functions:

//get links in string
function makeLinks($str) {
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    $urls = array();
    $urlsToReplace = array();
    if(preg_match_all($reg_exUrl, $str, $urls)) {
        $numOfMatches = count($urls[0]);
        $numOfUrlsToReplace = 0;
        for($i=0; $i<$numOfMatches; $i++) {
            $alreadyAdded = false;
            $numOfUrlsToReplace = count($urlsToReplace);
            for($j=0; $j<$numOfUrlsToReplace; $j++) {
                if($urlsToReplace[$j] == $urls[0][$i]) {
                    $alreadyAdded = true;
                }
            }
            if(!$alreadyAdded) {
                array_push($urlsToReplace, $urls[0][$i]);
            }
        }
        $numOfUrlsToReplace = count($urlsToReplace);
        for($i=0; $i<$numOfUrlsToReplace; $i++) {
            $str = str_replace($urlsToReplace[$i], "<div class=\"dont-break-out\"><a href=\"".$urlsToReplace[$i]."\" class=\"titled_url\" rel=\"nofollow\" target=\"_blank\">".get_title($urlsToReplace[$i])."</a></div> ", $str);
        }
        return $str;
    } else {
        return $str;
    }
}

and this is the function that gets the url title:

//get link title
function get_title($url){
  $str = file_get_contents_utf8($url);
  if(strlen($str)>0){
    $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
    preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
    $title_trimmed=trim($title[1]);
    if(!empty($title_trimmed)){
        return $title[1];
    }else{

    return $url;
}

} } and this is the utf-8 file_get_contents:

function file_get_contents_utf8($fn) {
     $content = file_get_contents($fn);
      return mb_convert_encoding($content, 'UTF-8',
          mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

any help please ?

Mikha Matta
  • 189
  • 3
  • 4
  • 16
  • please check file_get_contents_utf8 function some website not allowd to send the content , for this you can use curl alternative – aniket ashtekar Jun 28 '16 at 10:46
  • thanks @aniketashtekar for commenting, i tryed this:http://stackoverflow.com/questions/8540800/php-how-can-use-curl-instead-file-get-contents but the same problem...i think the problem is with make url function – Mikha Matta Jun 28 '16 at 11:07
  • can you post any specific example where the code is failed ? – aniket ashtekar Jun 28 '16 at 11:10
  • when i share a link like: http://xxxxxxxxxx.com/post/30 i can see the link title, but if i post: http://xxxxxxxxx.com/post.php?id=30, i can see it – Mikha Matta Jun 28 '16 at 11:18
  • when i am executing your code , its working fine with both the example, might be the problem with specific url that your passing to your function . – aniket ashtekar Jun 28 '16 at 12:44

0 Answers0