I want to prevent spaces in hyperlinks on a UGC site. I have written regular expression it works perfect except its not removing trailing space from link and anchor text.
Here is my code:
$text = '< a href = " http://www.examplesite.com/ "> Example site </a>';
$text = preg_replace('#(<(\s+)*a(\s+)*href(\s+)*=(\s+)*("|\')(\s+)*([^"]+)("|\')>(\s+)*([^<]+)(\s+)*</a>)#','<a href="$8">$11</a> ',$text);
Output
<a href="http://www.examplesite.com/ ">Example site </a>
URLs also contain spaces i.e. http://www.examplesite.com/blog/a page with space.html