Skip to content
Advertisement

ignoring img tags while searching for urls in string

I have the following code in which I am replacing urls with hyperlinked anchor tags,

public function urlify($string)
{

    //FIND URLS INSIDE TEXT
    //The Regular Expression filter
    $reg_exUrl = "/(((http(s?)(://))?([w]{3}.)?)([a-z0-9]+.)*([a-z|0-9])+.(com(.au)?|org|me|net|ly|be|gl|info|(co(.))?uk|ca|nz|tv)((/[^s]+)*)+)/i";

    // Check if there is a url in the text
    if (preg_match($reg_exUrl, $string, $url)) {

        if (strpos($url[0], ":") === false) {
            $link = 'http://' . $url[0];
        } else {
            $link = $url[0];
        }

        // make the urls hyper links
        $string = preg_replace($reg_exUrl, '<a href="' . $link . '" title="' . $url[0] . '" target="_blank">' . $url[0] . '</a>', $string);
    }

    return $string;
}

But I want to ignore all img tags while searching for urls, I have no idea how can I go for it.

Advertisement

Answer

As per @Biesor and @Nigel Ren I came up with this,

public function urlify($string)
{

    //FIND URLS INSIDE TEXT
    //The Regular Expression filter
    $reg_exUrl = "/(((http(s?)(://))?([w]{3}.)?)([a-z0-9]+.)*([a-z|0-9])+.(com(.au)?|org|me|net|ly|be|gl|info|(co(.))?uk|ca|nz|tv)((/[^s]+)*)+)/i";

    $htmlDom = new DOMDocument;
    $htmlDom->loadHTML($string);
    $imageTags = $htmlDom->getElementsByTagName('img');
    $img_srcs = array();
    foreach($imageTags as $imageTag){
        $img_srcs[] = $imageTag->getAttribute('src');
        $imageTag->setAttribute('src', ''); 
    }        
    $string = $htmlDom->saveHTML();

    // Check if there is a url in the text
    if (preg_match($reg_exUrl, $string, $url)) {

        if (strpos($url[0], ":") === false) {
            $link = 'http://' . $url[0];
        } else {
            $link = $url[0];
        }

        // make the urls hyper links
        $string = preg_replace($reg_exUrl, '<a href="' . $link . '" title="' . $url[0] . '" target="_blank">' . $url[0] . '</a>', $string);
    }

    foreach($imageTags as $key => $imageTag){
        $imageTag->setAttribute('src', $img_srcs[$key]); 
    }
    $string = $htmlDom->saveHTML();
    $p =  $htmlDom->getElementsByTagName('p')->item(0);
    if($p)
    { 
        $string = implode(
            "",
            array_map([$htmlDom, 'saveHTML'], iterator_to_array($p->childNodes))
        );
    }

    return $string;
}

For the time being I am using this till some better solution comes up.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement