php preg_replace 如何不改变url

发布于 2025-01-18 16:55:19 字数 508 浏览 1 评论 0原文

html 内容中的 ts 应替换为 s.

例如: sanktsya =>; sanksya

这就是它正常工作的方式。

aimg url 正在发生变化。

如何不更改网址?

我的代码

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

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2025-01-25 16:55:19

就像@nick所说的那样,这是DOM的任务。 DOM结构由不同的节点组成(元素,注释,文本节点,...)。使用XPath,您可以按类型来解决节点。

您的字符串看起来像XHTML片段字符串,因此以下是一个示例:

$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);

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);
む无字情书 2025-01-25 16:55:19

使用

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

请参阅正则表达式证明

说明

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'

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文