How do I open this function url in a new tab

Asked

Viewed 5,853 times

2

The code below converts url hyperlink text:


$bodyText = "The text you want to filter goes here.";
function formatUrlsInText($text) {
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    preg_match_all($reg_exUrl, $text, $matches);

    $usedPatterns = array();
    foreach($matches[0] as $pattern){
        if(!array_key_exists($pattern, $usedPatterns)){
            $usedPatterns[$pattern]=true;

            // now try to catch last thing in text
            $pattern2 = substr($pattern, -3);

            if($pattern2 == "gif" || $pattern2 == "peg" || $pattern2 == "jpg" || $pattern2 == "png") {
                 $text = str_replace($pattern, '', $text);
            } else {
                 $text = str_replace($pattern, ''.$pattern.'',      $text);
            }
        }
     }
     return $text;
}

$format = formatUrlsInText($bodyText);

echo $format;

My goal is to make the url it converts open in new tab. If it is very difficult accepted other functions.

1 answer

6


Use target on your link.

Insert target='_blank' in your code:

<a target="_blank" href="'.$pattern.'">'.$pattern.'</a>'

  • I work perfectly, Vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.