Page 1 of 1

Find Username And Link It

Posted: Fri Nov 11, 2011 8:19 pm
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>

Re: Find Username And Link It

Posted: Fri Nov 11, 2011 8:31 pm
by Celauran
Can you elaborate a little? Maybe show what you've written so far?

Re: Find Username And Link It

Posted: Fri Nov 11, 2011 8:33 pm
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)); ?>

Re: Find Username And Link It

Posted: Fri Nov 11, 2011 8:35 pm
by twinedev

Code: Select all

$strText = str_replace($row_users['username'],'<a href="user/'.$row_users['username'].'">'.$row_users['username'].'</a>',$strText);

Re: Find Username And Link It

Posted: Fri Nov 11, 2011 8:39 pm
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>

Re: Find Username And Link It

Posted: Fri Nov 11, 2011 8:53 pm
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.

Re: Find Username And Link It

Posted: Fri Nov 11, 2011 9:06 pm
by Celauran

Code: Select all

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