首页 > 解决方案 > 在字符串周围加上撇号?

问题描述

我试图在 PHP 中的字符串周围加上撇号。

我已经展示了每一步的结果。我不知道如何用 '' 包裹每个名字?

我可以做到这一点,Listen Up, Case Studies, Publications但我需要结果看起来像这样'Listen Up', 'Case Studies', 'Publications'

$categories = $wlrs_cats[0]['content_type']; 

$value = implode(',', $categories);
// $value = content-type/listen-up/,content-type/case-studies/,content-type/publications/

$result = ucwords(strtr($value, ["content-type/"=>" '", "/"=>"'", "-"=>" "]), "' ");
// $result; // 'Listen Up', 'Case Studies', 'Publications'

标签: php

解决方案


我会提出一个更简单的解决方案,$string您的原始解决方案在哪里content-type/listen-up/,content-type/case-studies/,content-type/publications/

$result = ucwords(strtr($string, ["content-type/"=>" '", "/"=>"'", "-"=>" "]), "' ");

因此,您替换某些内容,然后大写说明 ('space) 后面的单词。


推荐阅读