Only letters numbers and one special character at the end?

How can I check if the string contains only letters numbers and one last punctuation character if not error?

I am using this but it ain’t working:

if ($hashtag && !preg_match("/^[0-9a-zA-Z [:punct:]]+$/", $hashtag))
    echo "stopped";

if it’s not working you should ask PHP what went wrong: if that reveals no error then it’s back to the RegExp drawing board.

PS. your punctuation (as well as the other allowed characters) can be anywhere an as often appear in the string.

Could you please post a solution? What I am trying to do is to validate hash tag that has only one special character at the end?

#test -> pass
#test, -> pass
#23ok -> pass
#23ok, -> pass
#r.3r -> no pass

Wouldn’t you want

/^[0-9a-zA-Z ]+[[:punct:]]?$/

Seems to produce the results you expect

  1. 0-9a-zA-Z [:punct:] ↩︎

Thanks buddy very much.

How can I only allow numbers, letters and these symbols without the space: + , . :

  1. 0-9a-zA-Z ↩︎

Give me examples of what you consider valid and invalid input so I understand the question better.

Lets say #tes5t is a valid tag now I would like to check if the tag contains these char +,.: (only one) at the end if so pass else error?

Some like Facebook that auto strips special characters from hash tags?

Okay, so instead of [[:punct:]]? you want [+,.:]?

If you do not want to support spaces, you can remove the space from [0-9a-zA-Z ] (it is after the capital Z)

1 Like

Buddy thanks so much for your help. I am sorry but I don’t know how to use that tool could you please post here the Regular Expression

Buddy you nailed it works juts as I want it :wink: +100 thanks love you

1 Like

The regex is right in the middle, you simply copy and paste it into your PHP code and it should work.

Just to help you out, as this tool is REALLY AWESOME! Here are some tips

So #1 is your regular expression. You would put this in your code between your delimiters (typically /).
So your code would be preg_match("/^[0-9a-zA-Z ]+[+,.:]?$/", $hashtag) to match on it, !preg_match("/^[0-9a-zA-Z ]+[+,.:]?$/", $hashtag) to not match on it.

#2, is my test criteria, Blue indicates a match to the expression, white is no match. This is accomplished using the “mg” on the line of the regular expression. It treats each line as a single input to the expression, so I can quickly test all of the scenarios I want to pass/fail in a single shot.

You will see how any that end with multiple punctuation are not blue. Same with if it has punctuation in the middle of the letters/numbers.

#3 tells you what the regex is doing step by step, which is one reason I love this tool. As it not only helps you test your expressions, it informs you as to what the expression does.

Buddy again thanks so much.

Could you please help me to make this one ignore the special characters:


Some like this:


What is it matching now, that you do not want it to match?
And what is an example of what it should match?

I would only to parse the hash tag and not font color or url:

<div class="postbit_post">
        <blockquote><cite><span> (06-23-2015 05:31 PM)</span>q21051983 Wrote: <a class="quick_jump" href="http://localhost/howtomakemoney/Thread-testing-3?pid=17270#pid17270">&nbsp;</a></cite>test</blockquote>
<span style="color: #FF0000;">testing</span> #seo.

I like this one but it parses all the link and font colors. Please help me to avoid that:


Can you run the code through strip_tags to get rid of the tags? (as this will get very complex)

Could you please teach me how?

Something like

if ($hashtag && !preg_match("/^[0-9a-zA-Z ]+[+,.:]?$/", strip_tags($hashtag)))
    echo "stopped";

But I may need to see your actual code for this part, so I can inform you better

It removes a ton of content.

Buddy this works but when it detects a special characters it wont parse the link:

 function tag_parse($message) {

global $post, $mybb;
$tweet = $message;
return preg_replace("/#([A-Za-z0-9_]+)(?=\s|\Z)/", '<a href="'.$mybb->settings['bburl'].'/hashtag/\1">#\1</a>', $tweet);
return $message; 


Try this

 function tag_parse($message) {

global $post, $mybb;
$tweet = strip_tags($message);
return preg_replace("/#([A-Za-z0-9_]+)/", '<a href="'.$mybb->settings['bburl'].'/hashtag/\1">#\1</a>', $tweet);
return $message; 


Buddy thanks again but it removes a ton of content!

This one works but it doesn’t return the tag name only shows #

function tag_parse($message) {

global $post, $mybb;

$tweet = $message;
return preg_replace("/#[0-9a-zA-Z]+[+,.:]?$/", '<a href="'.$mybb->settings['bburl'].'/hashtag/\1">#\1</a>', $tweet);
return $message;