Jump to content

Recommended Posts

Hi again

i have a little coding problem, regarding regex, i want to write a code that is like bbcode but searches for user names in the content and replaces them with profile links, any idea how i can do this using regex??

 

Is it possible with regex, or shall i use something like a loop with str replace

Link to comment
https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/
Share on other sites

str replace works well with exact replacement but RegEx is more flexable,,

the problem is the lack on info..

you say you want to replace username with a hyperlink..

so i assume you have text with a bunch of info and some words are usernames and those you wish to create hyperlinks for..

 

if this is the case then your need to build an array of usernames at the same time build the hyperlinks for those users

ie

$Users = array("Blade280891", "MadTechie");

$UsersLink = array("<a href='user.php?name=Blade280891>Blade280891</a>", "<a href='user.php?name=MadTechie>MadTechie</a>");

 

then use

$data = str_replace($Users, $UsersLink, $data);

 

Now you could create a regEx with a callback etc etc but i think that maybe slower,

you could extract every word and remove dups then check in the userlist.. that may work but its a bit of a 50/50 game it really depends on quite a few things

 

my Solution about is based on alot of guess work!

Thats the solution i thought off but i am worried about speed issues as there could be a lot of users in the db.

 

I was thinking that it would loop through each user name and check the text for that username and replace it, but i assume this can be very slow?

Never mind, but i have got this now

	$code = preg_replace("/\[user=(.*?)\]/s","\\1",$code);

	$sql = mysql_query("SELECT id FROM user_details WHERE username = '".mysql_real_escape_string($code)."'") or die(mysql_error());

	if(mysql_num_rows($sql) == 0)
		$code = $code;
	else
	{
		$code = CreateUserLink($code);
	}

 

But it isn't creating the link :s

No that code is fine, the fact is that after debugging i found out that the sql query is returning 0 rows although if i do it myself it doesn't, this is because i believe all the other text in the $code var is being sent as well, not just the bit i need

if your doing a lookup then i assume you mean this!

if (preg_match('/\[user=(.*?)\]/s', $code, $regs))
{
$sql = mysql_query("SELECT id FROM user_details WHERE username = '".mysql_real_escape_string($regs[1])."'") or die(mysql_error());
}

 

however.. i'm not sure why your creating a link as the lookup would be from the html the link would of already be created!  ???

Not working, i tried this

 

	if(preg_match('/\[user=(.*?)\]/s', $code, $regs))
	{
	    $sql = mysql_query("SELECT id FROM user_details WHERE username = '".mysql_real_escape_string($regs[1])."'");

		if(mysql_num_rows($sql) == 0)
			$code = preg_replace("/\[user=(.*?)\]/s","\\1",$code);
		else
		{

			$code = preg_replace("/\[user=(.*?)\]/s",CreateUserLink("\\1"),$code);
		}
	}	

 

The bit where the link should be is empty

Sorry , i tried to only show code which applied to keep it simple, but this code is inside one bigger function which is then inside another function.

 

So here is the bbcode function

<?php
Removed 
?>

The output i need is a link to the users profile, the function createuserlink will create a link to the profile based on username or userid.

 

the function createuserlink has these arguments

function CreateUserLink($userid = null,$username = null)

 

if $userid is null then it creates one using $username

 

Scrap all that, solved i just needed to change

$code = preg_replace("/\[user=(.*?)\]/s",CreateUserLink(null,"\\1",$code);

 

to

 

$code = preg_replace("/\[user=(.*?)\]/s",CreateUserLink(null,$regs[1]),$code);

 

No, not yet, found a bug

 

this code

	if(preg_match('/\[user=(.*?)\]/s', $code, $regs))
	{
		$sql = mysql_query("SELECT username FROM user_details WHERE username = '".mysql_real_escape_string($regs[1])."'");
		$user = mysql_fetch_assoc($sql);

		if(mysql_num_rows($sql) == 0)
			$code = preg_replace("/\[user=(.*?)\]/s","\\1",$code);
		else
		{

			$code = preg_replace("/\[user=(.*?)\]/s",CreateUserLink(null,$user['username']),$code);
		}
	}	

	if(preg_match('/\[user\](.*?)\[\/user\]/s', $code, $regs))
	{
	    $sql = mysql_query("SELECT username FROM user_details WHERE username = '".mysql_real_escape_string($regs[1])."'");
		$user = mysql_fetch_assoc($sql);

		if(mysql_num_rows($sql) == 0)
			$code = preg_replace("/\[user\](.*?)\[\/user\]/s","\\1",$code);
		else
		{

			$code = preg_replace("/\[user\](.*?)\[\/user\]/s",CreateUserLink(null,$user['username']),$code);
		}
	}	

 

when i do

[user]blade280891[/user]

[user]protestthehero[/user]

 

[user=blade280891]

[user=protestthehero]

 

It makes four links which is correct, but the second and last link is to the first one if that makes sense . SO the above prints

 

Blade280891

Blade280891

 

Blade280891

Blade280891

Any ideas?

But this should be fine!!!

 

$code = preg_replace('/\[user=(.*?)\]/sim', '<a href=\"profile.php?user=\1\" target=\"_blank\">\1</a>', $code);
$code = preg_replace('%\[user\](.*?)\[/user\]%sim', '<a href=\"profile.php?user=\1\" target=\"_blank\">\1</a>', $code);

 

[user]blade280891[/user]

[user]protestthehero[/user]

 

[user=blade280891]

[user=protestthehero]

 

<a href=\"profile.php?user=\" target=\"_blank\"></a>

<a href=\"profile.php?user=\" target=\"_blank\"></a>

 

<a href=\"profile.php?user=blade280891\" target=\"_blank\">blade280891</a>

<a href=\"profile.php?user=protestthehero\" target=\"_blank\">protestthehero</a>

Surely thats easy to fix!

 

in anycase try this

 

<?php
$code  = preg_replace_callback('/(\[user=(.*?)\])/sim', "GetID", $code);

function GetID($User)
{
$sql = mysql_query("SELECT ID FROM user_details WHERE username = '".mysql_real_escape_string($User[2])."' LIMIT 1");
$user = mysql_fetch_assoc($sql);
if(mysql_num_rows($sql) == 0)
{
return "<a href=\"profile.php?user=$user['ID']\" target=\"_blank\">$user['ID']</a>;";
}else{
return "Invalid User!";
}

}
?>

Errors

 

07:56 am:

Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'GetID', to be a valid callback in /home/vheissu/public_html/includes/core.php on line 114

07:56 am:

Fatal error: Cannot redeclare getid() (previously declared in /home/vheissu/public_html/includes/core.php:116) in /home/vheissu/public_html/includes/core.php on line 116

Their was a few typos but it works fine

 

*tested*

<?php
$code = "[user=blade280891]
[user=protestthehero]";
$code  = preg_replace_callback('/(\[user=(.*?)\])/sim', "GetID", $code);
echo $code;

function GetID($User)
{
$sql = mysql_query("SELECT ID FROM user_details WHERE username = '".mysql_real_escape_string($User[2])."' LIMIT 1");
$user = mysql_fetch_assoc($sql);
if(mysql_num_rows($sql) != 0)
{
	return "<a href=\"profile.php?user={$user['ID']}\" target=\"_blank\">{$user['ID']}</a>;";
}else{
	return "Invalid User!";
}
}
?>

Same errors, this is my code

 

$code  = preg_replace_callback('/(\[user=(.*?)\])/sim', "Get_my_ID", $code);

 

function Get_my_ID($User)

{

  $sql = mysql_query("SELECT id FROM user_details WHERE username = '".mysql_real_escape_string($User[2])."' LIMIT 1");

  $user = mysql_fetch_assoc($sql);

  if(mysql_num_rows($sql) != 0)

  {

      return "<a href=\"profile.php?user={$user['id']}\" target=\"_blank\">{$user['id']}</a>;";

  }else{

      return "Invalid User!";

  }

}

 

 

It inside a function, does that effect it?

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.