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
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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.