Home > Blockchain >  php preg_replace how not changing urls
php preg_replace how not changing urls

Time:04-04

ts in html content should be replaced by s.

for example: sanktsya => sanksya

That's how it works fine.

But a or img urls are changing.

how to not changing urls ?

My code

$str='<p>sanktsya boshlandi.</p>
<p><img src="https://img.com/Sqstso.jpeg" /></p>';

$result = preg_replace("/([qwrtpsdfghklzxcvbnmQWRTPSDFGHKLZXCVBNM] )[tT]s/", "\${1}s", $str);

CodePudding user response:

Like @nick said this is a task for DOM. A DOM structure consists of different nodes (elements, comments, text nodes, ...). With Xpath you can address nodes by type.

You string looks like an XHTML fragment string so here is an example:

$xhtml = <<<'XHTML'
<p>sanktsya boshlandi.</p>
<p><img src="https://img.com/Sqstso.jpeg" /></p>
XHTML;

// bootstrap DOM
$document = new DOMDocument();
$xpath = new DOMXpath($document);

// create a fragment node and append XML content
$fragment = $document->createDocumentFragment();
$fragment->appendXML($xhtml);

// iterate any descendant text node
foreach($xpath->evaluate('.//text()', $fragment) as $textNode) {
    // modify node text content
    $textNode->textContent = preg_replace(
        '(([qwrtpsdfghklzxcvbnmQWRTPSDFGHKLZXCVBNM] )[tT]s)', 
        '$1s',
        $textNode->textContent
    );
}

// save XHTML fragment string
echo $document->saveXML($fragment);

CodePudding user response:

Use

$result = preg_replace("/<[^>]*>(*SKIP)(*FAIL)|[qwrtpsdfghklzxcvbnm] \Kts/i", "s", $str);

See regex proof.

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  <                        '<'
--------------------------------------------------------------------------------
  [^>]*                    any character except: '>' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  >                        '>'
--------------------------------------------------------------------------------
 (*SKIP)(*FAIL)            Skip current match, resume search from current position
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  [qwrtpsdfghklzxcvbnm     any character of: 'q', 'w', 'r', 't', 'p',
  ]                        's', 'd', 'f', 'g', 'h', 'k', 'l', 'z',
                           'x', 'c', 'v', 'b', 'n', 'm' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  \K                       Match reset operator, discards text from match
--------------------------------------------------------------------------------
  ts                       'ts'
  • Related