My question is the following: I need to set curly brackets in the first two characters in a string
$string = "We want home";
echo $string;
I want to echo this:
(We) Want home
Thanks in advance.
My question is the following: I need to set curly brackets in the first two characters in a string
$string = "We want home";
echo $string;
I want to echo this:
(We) Want home
Thanks in advance.
$s='We want to go home';
echo preg_replace('@^(\w{2})@','($1)',$s );
One solution:
<?php
$string = "We want home";
$parts = explode(' ', $string);
$parts[0] = '('.$parts[0].')';
$string = implode($parts, ' ');
var_dump($string); // outputs "(We) want home"
You could as well use regex
.