Code Duplication    Length = 35-35 lines in 2 locations

src/voku/helper/AbstractDomParser.php 1 location

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

src/voku/helper/HtmlDomParser.php 1 location

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