ExternalLinkFilter::removeWebLinks()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
namespace ExternalLinkPurifier\src\Services;
4
5
class ExternalLinkFilter {
6
7
    /**
8
     * @param string $content
9
     * @param string $domain    //Keep links of this domain remove others
10
     * 
11
     * @return string
12
     */
13
    public static function removeWebLinks($content, $domain = '') {
14
        if (!empty($content)) {
15
            if (!empty($domain)) {
16
                $pattern = '#<a [^>]*\bhref=([\'"])http.?://((?<!'. $domain .')[^\'"])+\1 *.*?</a>#i';                
17
                $filteredString = preg_replace($pattern, '', $content);
18
            } else {
19
                $filteredString = preg_replace('#<a.*?>.*?</a>#i', '', $content);
20
            }
21
            
22
            return $filteredString;
23
        }        
24
    }
25
}
26