My string is
$string = ",name2,name2,name3,"; I want to make it like;
$string = "name2,name2,name3"; That is, to remove first and last comma from that string, any clue as to how to accomplish this either through regex or anything else?
Thanks.
You can use anchors for this:
$result = preg_replace('/^,|,$/', '', $subject); If you want to match one or more commas at the start/end of the string:
$result = preg_replace('/^,+|,+$/', '', $subject); And if there could be whitespace around those leading/trailing commas:
$result = preg_replace('/^[,\s]+|[\s,]+$/', '', $subject);