gpt4 book ai didi

php - 如何在 PHP 中替换第一个 HTML 标签

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:05:55 27 4
gpt4 key购买 nike

我有一个 PHP 文本字符串:

<strong> MOST </strong> of you may have a habit of wearing socks while sleeping. 
<strong> Wear socks while sleeping to prevent cracking feet</strong>
<strong> Socks helps to relieve sweaty feet</strong>

我们可以看到,第一个强标签是

<strong> MOST </strong>

我想删除第一个强标签,并将其中的单词变成ucwords(首字母大写)。这样的结果

Most of you may have a habit of wearing socks while sleeping. 
<strong> Wear socks while sleeping to prevent cracking feet</strong>
<strong> Socks helps to relieve sweaty feet</strong>

我试过爆炸功能,但它似乎不是我想要的。这是我的代码

<?php
$text = "<strong>MOST</strong> of you may have a habit of wearing socks while sleeping. <strong> Wear socks while sleeping to prevent cracking feet</strong>. <strong> Socks helps to relieve sweaty feet</strong>";
$context = explode('</strong>',$text);
$context = ucwords(str_replace('<strong>','',strtolower($context[0]))).$context[1];
echo $context;
?>

我的代码唯一的结果

Most of you may have a habit of wearing socks while sleeping. <strong> Wear socks while sleeping to prevent cracking feet

最佳答案

您可以使用 explode 的可选 limit 参数修复您的代码:

$context = explode("</strong>",$text,2);

但是,这样会更好:

$context = preg_replace_callback("(<strong>(.*?)</strong>)",function($a) {return ucfirst($a[1]);},$text);

关于php - 如何在 PHP 中替换第一个 HTML <strong></strong> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14658732/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com