在PHP中的第一个逗号之后删除所有字符

根据此帖子remove everything after first comma from string in php

我需要在Wordpress中自定义此行:

<div class="text-center font-weight-bold" style="font-size: 16px; line-height: 16px; text-decoration: underline;">
    <?php 
        echo rtrim( wp_trim_words( get_the_title(),2,'' ),',' ); 
    ?>
</div>

它的效果很好,但是某些产品标题的长度必须超过两个字,因此我需要在逗号字符之前输入所有字。

dmayahot777 回答:在PHP中的第一个逗号之后删除所有字符

您也可以使用preg_replace

$f = "Hello there,im a post title,and some stuff";

echo preg_replace("/,.+/","",$f); // Hello there
,

尝试使用explode()

<div class="text-center font-weight-bold" style="font-size: 16px; line-height: 16px; text-decoration: underline;">
    <?php 
        echo explode(',',get_the_title())[0]; 
    ?>
</div>
本文链接:https://www.f2er.com/3166788.html

大家都在问