<?php
$text = 'Some text with #hash_tag #tags and #other2 things';
$expected = 'Some text with <a href="hash_tag">#hash_tag</a> <a href="tags">#tags</a> and <a href="other2">#other2</a> things';
$text = preg_replace('/\#(\d+|\w+|_)/', '<a href="$1">#$1</a>', $text);
var_dump($text);
var_dump($text === $expected);