Find Username And Link It

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanweekly
Forum Newbie
Posts: 3
Joined: Sat Apr 30, 2011 1:37 pm

Find Username And Link It

Post by ryanweekly »

How would I start with a code that finds a username ($row_users['username']) and adds <a href="user/username">Original Text</a>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Find Username And Link It

Post by Celauran »

Can you elaborate a little? Maybe show what you've written so far?
ryanweekly
Forum Newbie
Posts: 3
Joined: Sat Apr 30, 2011 1:37 pm

Re: Find Username And Link It

Post by ryanweekly »

I am fairly new to php.
The username I would like to be linked would be in the string $row_Recordset1['username']

Code: Select all

<?PHP do { ?>
                                     <li>
                                        	<div class="line"></div>
                                            <div class="image"><a href="#"><img src="<?PHP include ("includes/cdata.php"); ?>" width="61" height="61" alt="avatar"></a></div>
                                            
                                            <div class="details">
                                            
                                                <div class="name"><span class="author"><a href="#"><?PHP echo $row_Recordset1['username']; ?></a></span> <span class="date"><?PHP echo $row_Recordset1['time']; ?></span></div>
                                                <p><?PHP echo $row_Recordset1['content']; ?></p>
                                                
                                            </div><!--details-->
                                        </li>
                                         <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
Last edited by ryanweekly on Fri Nov 11, 2011 8:37 pm, edited 1 time in total.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Find Username And Link It

Post by twinedev »

Code: Select all

$strText = str_replace($row_users['username'],'<a href="user/'.$row_users['username'].'">'.$row_users['username'].'</a>',$strText);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Find Username And Link It

Post by Celauran »

Unless I'm completely misunderstanding what you're trying to do, replace this:

Code: Select all

<a href="#"><?PHP echo $row_Recordset1['username']; ?></a>
with this:

Code: Select all

<a href="users/<?php echo $row_Recordset1['username']; ?>"><?php echo $row_Recordset1['username']; ?></a>
ryanweekly
Forum Newbie
Posts: 3
Joined: Sat Apr 30, 2011 1:37 pm

Re: Find Username And Link It

Post by ryanweekly »

Maybe this will clear things up a little bit. Lets say a user posts a comment that says "You should check out [user]rfil[/user]" and "rfil" is a username. I would like my php script to link that username. Sorry for not being as clear in my first post.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Find Username And Link It

Post by Celauran »

Code: Select all

$string = "[user]Whatever[/user]";
$string = preg_replace('/\[user\](.*)?\[\/user\]/', '<a href="users/\1">\1</a>', $string);
echo $string;
Post Reply