Issue
Here is code:
$string="{1},[2],(3),<4>";
// Replaces closing square, curly, angle brackets with round brackets
$string = preg_replace('/\{\[\</', '(', $string);
$string = preg_replace('/\}\]\>/', ')', $string);
It didn't replace at all in that string... Any better coding than that?
Thanks.
Solution
{[<
never occurs in your string. Use a character class or optional grouping.
$string = preg_replace('/[{[<]/', '(', $string);
$string = preg_replace('/[}>\]]/', ')', $string);
Alternative non character class approach:
$string = preg_replace('/(?:\{|<|\[)/', '(', $string);
$string = preg_replace('/(?:\}|>|\])/', ')', $string);
Answered By - user3783243 Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.