我的问题如下: 我需要在字符串的前两个字符中设置花括号
$string = "We want home";
echo $string;
我想回应一下:
(We) Want home
提前致谢。
答案 0 :(得分:1)
$s='We want to go home';
echo preg_replace('@^(\w{2})@','($1)',$s );
答案 1 :(得分:0)
一个解决方案:
<?php
$string = "We want home";
$parts = explode(' ', $string);
$parts[0] = '('.$parts[0].')';
$string = implode($parts, ' ');
var_dump($string); // outputs "(We) want home"
您也可以使用regex
。