首页 > 解决方案 > 删除PHP中第一个逗号后的所有字符

问题描述

根据这篇文章从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>

效果很好,但有些产品标题需要超过两个单词,所以我需要逗号字符之前的所有单词。

标签: phpwordpress

解决方案


你也可以使用preg_replace

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

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

推荐阅读