删除除字符串的连字符和下划线之外的所有标点符号

I want to remove all punctuation from beginning and end of a string except hyphen,underscore.

Example:if input is spice-b32. Or lg_b32; Then string after using preg_replace(); should be: spice-b32 and lg_b32;

i'm also tried to use preg_match('/^[A-Za-z0-9]/',$inm) for data validation use $inm=preg_replace('/^\PL+|\PL\z/','',$inm); but, when input a!-read_ result is a!-read

but output should be: a-read

if this preg_replace() OR preg_match() is not correct,then plz help..

If I understand correctly what you want, then something like this will do for you:

$inm=preg_replace('/[,.!?]*([-_]+)[,.!?]*/',
                  '\1',
                  preg_replace('/\b[.,?!]+|[.,!?]+\b/', '', $inm);

Feel free to add other characters that need to be stripped off to the character groups.

How about

$arr = array('spice-b32.', 'lg_b32;', 'a!-read_');
foreach ($arr as $str) {
    echo preg_replace('/^[^\P{P}_-]+|[^\P{P}_-]+$/u', '', $str),"
";
}

This will remove all punctuation (except _ and -) from the begining or end of a string.

output:

spice-b32
lg_b32
a!-read_