php - 删除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>
效果很好,但有些产品标题需要超过两个单词,所以我需要逗号字符之前的所有单词。
解决方案
你也可以使用preg_replace
$f = "Hello there, im a post title, and some stuff";
echo preg_replace("/,.+/", "", $f); // Hello there
推荐阅读
- c# - 应用程序洞察读取响应正文
- r - 当冒号做同样的事情时,为什么要使用 c 创建向量?
- amazon-web-services - 如何在 amazon cognito 的多因素身份验证上设置“mfa_setup”挑战?
- php - phpMyAdmin - 错误格式参数不正确
- django - 一个 Select Widget,其字段为复选框,可能有多个选项
- java - 如何解决以下代码中的 java.lang.NoClassDefFoundError?
- java - 在多核 CPU 上运行的多个 JVM 会相互阻塞吗?
- perl - 如何使 uc() 在 Perl 中像 toupper() 在区域感知 POSIX 环境中一样在 AWK 中工作?
- java - 尝试代码时出现此错误:线程“main”java.lang.NullPointerException 中的异常。有人能帮我吗?
- javascript - 我可以在 mat-expanded 行中显示另一个表吗?