Code Duplication    Length = 35-35 lines in 2 locations

src/voku/helper/HtmlDomParser.php 1 location

@@ 711-745 (lines=35) @@
708
     *
709
     * @return string
710
     */
711
    public static function replaceToPreserveHtmlEntities(string $html): string
712
    {
713
        // init
714
        $linksNew = [];
715
        $linksOld = [];
716
717
        if (\strpos($html, 'http') !== false) {
718
719
            // regEx for e.g.: [https://www.domain.de/foo.php?foobar=1&email=lars%40moelleken.org&guid=test1233312&{{foo}}#foo]
720
            $regExUrl = '/(\[?\bhttps?:\/\/[^\s<>]+(?:\([\w]+\)|[^[:punct:]\s]|\/|\}|\]))/i';
721
            \preg_match_all($regExUrl, $html, $linksOld);
722
723
            if (!empty($linksOld[1])) {
724
                $linksOld = $linksOld[1];
725
                foreach ((array) $linksOld as $linkKey => $linkOld) {
726
                    $linksNew[$linkKey] = \str_replace(
727
                        self::$domLinkReplaceHelper['orig'],
728
                        self::$domLinkReplaceHelper['tmp'],
729
                        $linkOld
730
                    );
731
                }
732
            }
733
        }
734
735
        $linksNewCount = \count($linksNew);
736
        if ($linksNewCount > 0 && \count($linksOld) === $linksNewCount) {
737
            $search = \array_merge($linksOld, self::$domReplaceHelper['orig']);
738
            $replace = \array_merge($linksNew, self::$domReplaceHelper['tmp']);
739
        } else {
740
            $search = self::$domReplaceHelper['orig'];
741
            $replace = self::$domReplaceHelper['tmp'];
742
        }
743
744
        return \str_replace($search, $replace, $html);
745
    }
746
747
    /**
748
     * Get the HTML as XML or plain XML if needed.

src/voku/helper/AbstractDomParser.php 1 location

@@ 441-475 (lines=35) @@
438
     *
439
     * @return string
440
     */
441
    public static function replaceToPreserveHtmlEntities(string $html): string
442
    {
443
        // init
444
        $linksNew = [];
445
        $linksOld = [];
446
447
        if (\strpos($html, 'http') !== false) {
448
449
            // regEx for e.g.: [https://www.domain.de/foo.php?foobar=1&email=lars%40moelleken.org&guid=test1233312&{{foo}}#foo]
450
            $regExUrl = '/(\[?\bhttps?:\/\/[^\s<>]+(?:\([\w]+\)|[^[:punct:]\s]|\/|\}|\]))/i';
451
            \preg_match_all($regExUrl, $html, $linksOld);
452
453
            if (!empty($linksOld[1])) {
454
                $linksOld = $linksOld[1];
455
                foreach ((array) $linksOld as $linkKey => $linkOld) {
456
                    $linksNew[$linkKey] = \str_replace(
457
                        self::$domLinkReplaceHelper['orig'],
458
                        self::$domLinkReplaceHelper['tmp'],
459
                        $linkOld
460
                    );
461
                }
462
            }
463
        }
464
465
        $linksNewCount = \count($linksNew);
466
        if ($linksNewCount > 0 && \count($linksOld) === $linksNewCount) {
467
            $search = \array_merge($linksOld, self::$domReplaceHelper['orig']);
468
            $replace = \array_merge($linksNew, self::$domReplaceHelper['tmp']);
469
        } else {
470
            $search = self::$domReplaceHelper['orig'];
471
            $replace = self::$domReplaceHelper['tmp'];
472
        }
473
474
        return \str_replace($search, $replace, $html);
475
    }
476
}
477