How can I allow single hyphens and single spaces only within words but not at the beginning or at the end of the words?
if(!preg_match('/^[a-zA-Z0-9\-\s]+$/', $pg_tag))
{
$error = true;
echo '<error elementid="pg_tag" message="TAGS - only alphanumbers and hyphens are allowed."/>';
}
I don’t want to accept these inputs below
---stack---over---flow---
stack-over-flow- stack-over-flow2
stack over flow
but only these are acceptable,
stack-over-flow stack-over-flow2 stack-over-flow3
stack over flow
stacoverflow
Thanks.
And the output:
A breakdown of the Regex: