Auto-linking Reddit References

Looking for an Extension? Have an Extension request? Post your request here for help. (Note: This forum is community supported; while there is an Extensions Development Team, said team does not dedicate itself to handling requests in this forum)
Scam Warning
User avatar
JoshyPHP
Code Contributor
Posts: 1164
Joined: Mon Jul 11, 2011 12:28 am

Re: Auto-linking Reddit References

Post by JoshyPHP »

AbaddonOrmuz wrote:
Wed Dec 12, 2018 6:19 am
It checks that the username and subreddit does not start with _
More precisely, #\br/([^_]\w+)\b# checks that it starts with anything except _. That includes whitespace and other things you wouldn't want in the username. Since this is just a test, it's better to use a lookahead assertion, i.e. #\br/(?!_)(\w+)\b#

Also, if I read correctly, usernames can use dashes so the regexp should probably be different. And you should definitely give names to the captures otherwise default ones will be chosen for you behind the scenes. The PHP line should be something like that:

Code: Select all

'#\\bu/(?![-_])(?<username>[-\\w]+)\\b#',
I wrote the thing that does BBCodes in 3.2+.
User avatar
AbaddonOrmuz
Recognised Extension Developer
Posts: 1002
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo
Contact:

Re: Auto-linking Reddit References

Post by AbaddonOrmuz »

JoshyPHP wrote:
Wed Dec 12, 2018 1:00 pm
It checks that the username and subreddit does not start with _

More precisely, #\br/([^_]\w+)\b# checks that it starts with anything except _. That includes whitespace and other things you wouldn't want in the username. Since this is just a test, it's better to use a lookahead assertion, i.e. #\br/(?!_)(\w+)\b#

Also, if I read correctly, usernames can use dashes so the regexp should probably be different. And you should definitely give names to the captures otherwise default ones will be chosen for you behind the scenes. The PHP line should be something like that:

Code: Select all

'#\\bu/(?![-_])(?<username>[-\\w]+)\\b#',
Right, I didn't test it with whitespaces, that would break things.

Trying to improve the regex even further, it should be limited to a range of 3-20 characters (Reddit's limit), otherwise it could match very long words.

Code: Select all

#\\bu/(?![_-])(?<user>[\\w-]{3,20})\\b#
Some of my phpBB extensions:
Image Imgur | :chart_with_upwards_trend: SEO Metadata | Image Markdown | :lock: Auto-lock Topics
:trophy: Check out all my validated extensions :trophy:

:penguin: Arch Linux user :penguin:
Post Reply

Return to “Extension Requests”