preg_replace 之间 >且<

发布于 2024-10-06 05:07:03 字数 377 浏览 4 评论 0 原文

我有这个:

<div> 16</div>

并且我想要这个:

<div><span>16</span></div>

目前,这是我可以使其工作的唯一方法:

preg_replace("/(\D)(16)(\D)/", "$1$2 $3", "

16
")

如果我省略 $3,我会得到:

<div><span>16</span>/div>

I have this:

<div> 16</div>

and I want this:

<div><span>16</span></div>

Currently, this is the only way I can make it work:

preg_replace("/(\D)(16)(\D)/", "$1<span>$2</span>$3", "<div> 16</div>")

If I leave off the $3, I get:

<div><span>16</span>/div>

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

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

发布评论

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

评论(4

冷默言语 2024-10-13 05:07:03

不太确定您的需求是什么,但以下内容更通用:

$value = "<div>    16    </div>";
echo(preg_replace('%(<(\D+)[^>]*>)\s*([^\s]*)\s*(</\2>)%', '\1<span>\3</span>\4', $value));

这会导致:

<div><span>16</span></div>

即使该值是:

<p>    16    </div>

它会导致:

<p><span>16</span></p>

Not quite sure what your after, but the following is more generic:

$value = "<div>    16    </div>";
echo(preg_replace('%(<(\D+)[^>]*>)\s*([^\s]*)\s*(</\2>)%', '\1<span>\3</span>\4', $value));

Which would result in:

<div><span>16</span></div>

Even if the value were:

<p>    16    </div>

It would result in:

<p><span>16</span></p>
○愚か者の日 2024-10-13 05:07:03

我认为您的意思是说您正在使用以下内容:

print preg_replace("/(\\D+)(16)(\\D+)/", "$1<span>$2</span>$3", "<div>16</div>");

这没有任何问题。 $3 将包含第二个 (\D+) 组中匹配的所有内容。如果你不去管它,显然它不会神奇地出现。

请注意,问题中的代码有一些错误:

  1. 您需要在字符串中转义 \
  2. 您需要使用 \D+ 来匹配多个字符。
  3. 字符串中的 16 之前有一个空格,但您没有在正则表达式中考虑到这一点。我删除了空格,但如果您想允许它,您应该使用 \s* 来匹配任意数量的空白字符。
  4. 您的参数顺序不正确。

I think you meant to say you're using the following:

print preg_replace("/(\\D+)(16)(\\D+)/", "$1<span>$2</span>$3", "<div>16</div>");

There's nothing wrong with that. $3 is going to contain everything matched in the second (\D+) group. If you leave it off, obviously it's not going to magically appear.

Note that your code in the question had some errors:

  1. You need to escape your \'s in a string.
  2. You need to use \D+ to match multiple characters.
  3. You have a space before 16 in your string, but you're not taking this into account in your regex. I removed the space, but if you want to allow for it you should use \s* to match any number of whitespace characters.
  4. The order of your parameters was incorrect.
凉城已无爱 2024-10-13 05:07:03

尝试以下操作 -

$str = "<div class=\"number\"> 16</div>";
$formatted_str = preg_replace("/(<div\b[^>]*>)(.*?)<\/div>/i", "$1<span>$2</span></div>", $s);
echo $formatted_str;

Try following -

$str = "<div class=\"number\"> 16</div>";
$formatted_str = preg_replace("/(<div\b[^>]*>)(.*?)<\/div>/i", "$1<span>$2</span></div>", $s);
echo $formatted_str;
水水月牙 2024-10-13 05:07:03

这就是最终效果最好的:

preg_replace('/(<.*>)\s*('. $page . ')\s*(<.*>)/i', "$1" . '<span class="curPage">' . "$2" . '</span>' . "$3", $pagination)

我发现我不确定页码之前或之后有哪些标签。

This is what ended up working the best:

preg_replace('/(<.*>)\s*('. $page . ')\s*(<.*>)/i', "$1" . '<span class="curPage">' . "$2" . '</span>' . "$3", $pagination)

What I found was that I didn't know for sure what tags would precede or follow the page number.

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