Jump to content

how to make @username to a link?


faybe

Recommended Posts

Hey,

 

What i want is that if a username inserts some text into a field like "Hey @martin how are ya" ? then it will display @martin as a link that takes you to site.com/martin, i searched php.net from preg_replace and str_replace...unfortunately for me things got so complicated that i don't even want to talk about it.

 

Anyone who can do it will be my saviour!

Link to comment
https://forums.phpfreaks.com/topic/210782-how-to-make-username-to-a-link/
Share on other sites

You wont find an exact example of what you're trying to achive within the manual. But you are looking at the right functions to use, which was preg_replace. You'll need to use regex to convet @username to a clickable to link which takes them to the uses profile.

 

Example code

$str = 'Hello, @martin How are you?';
$str = preg_replace('~@([a-z0-9]+)~i', '<a href="http://site.com/$1">@$1</a>', $str);

echo $str;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.