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

@@ 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.