954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Twitter taggin

Hey im new around here so if i get something wrong tell me :D

I have this code

$txt = str_replace("#", "<a href=user.php?user=$txt></a>", $txt);


I want it to when a user tags someone using # it goes to the user.php?user=WHATEVER#WAS. Its just like twitter is this possible and simple?

iroyal
Newbie Poster
4 posts since Dec 2010
Reputation Points: 24
Solved Threads: 0
 

str_replace is great for replacements but you have a pattern to find and section of that pattern you want to reuse. In your example, you insert the ENTIRE original string back into each link. YOu also lose the name for description of the link.

A regular expression does the job when your needs are more complicated:

<?php
$txt="The quick brown #fox jumps over the lazy #dog.";

$try2 = preg_replace('/#([A-Za-z][A-Za-z0-9]+)\b/i','<a href="/user.php?u=$1">$1</a>', $txt);
$txt);

echo "\n\n".$try2;
?>


A quick explanation of this regular expression in case you aren't very familiar with them: /#([A-Za-z][A-Za-z0-9]+)\b/i

Find the # sign followed immediately by a letter followed immediately by 1 or more letters or numbers. Stop selecting the pattern when we reach a word boundary (\b). The first and last '/' slashes are delimiters. The 'i' at the end means to search without regard to upper or lower case.

The portion in parentheses will be held onto as areference. That reference ($1) in our case can then be used twice in the replace for the query-string parameter and link text. (Think of copy and paste. $1 is the first entry in the virtual clipboard.)

* This regular expression could be refined a bit but I'm going with this one for readability purposes.

Hope it helps!

madCoder
Junior Poster
145 posts since Feb 2010
Reputation Points: 21
Solved Threads: 37
 

Nope that just duplicates the entry for $txt for example #admin would be admin#admin i have this code here

if(preg_match('/^\@[a-zA-Z0-9]+/', $txt)){
			$words = explode(' ', $txt);
			$username = substr($words[0], 1, strlen($words[0]));
			$username = str_replace(":", "", $username);
			$username = str_replace("-", "", $username);
			$username = str_replace(";", "", $username);
			$txt = str_replace("@$username", "@<a href='profile/$username'>$username</a>", $txt);
		}
		return $txt;



THat only looks for 1 @ symbol when i want to to look for many :s

iroyal
Newbie Poster
4 posts since Dec 2010
Reputation Points: 24
Solved Threads: 0
 

Actually that code does work but it wont search for it if there are words before @$username for example

this wont work @admin
@admin though this would

iroyal
Newbie Poster
4 posts since Dec 2010
Reputation Points: 24
Solved Threads: 0
 

It worked for me using php 5.3 with the test string I included in the sample. By default preg_replace() replaces all matches unless a limit is set. It doesn't search for the @at sign as that wasn't presented in your original example. For the @ sign change the regex to this:

/\b[#@]([A-Za-z][A-Za-z0-9]+)\b/i

Your expression would also, basically work if you remove the '^', which means "starts with". That expression explicitly says the string must begin with an @ to match.

The one I wrote also insists that the first character to follow the # or @ must be a letter. That way a string like, "You're #1" won't match.

NOTE: I added a \b in front of the expression to prevent a match on an email address.

* I'm not at my home computer right now, so I haven't tested the expressions in this posting.

madCoder
Junior Poster
145 posts since Feb 2010
Reputation Points: 21
Solved Threads: 37
 

Thanks i already have the solution from another site now though :D

iroyal
Newbie Poster
4 posts since Dec 2010
Reputation Points: 24
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: