DeanWhitehouse Posted April 3, 2009 Share Posted April 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/ Share on other sites More sharing options...
MadTechie Posted April 3, 2009 Share Posted April 3, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-800726 Share on other sites More sharing options...
DeanWhitehouse Posted April 3, 2009 Author Share Posted April 3, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-800732 Share on other sites More sharing options...
MadTechie Posted April 4, 2009 Share Posted April 4, 2009 Well Str_replace or RegEx either way you need to know the UserNames ie $data = preg_replace('/(Blade280891|MadTechie)/i', '<a href=\'user.php?name=\1>\1</a>', $data); your need someway to identify its a username! Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-800838 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 Ok, so i am going to use a user tag e.g. [user = ] but what do i do to find the user? Cus i have $code = preg_replace("/\[user=(.*?)\]","<a href=\"profile.php?user=\\1\" target=\"_blank\">\\1</a>",$code); Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804359 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 And the problem is ? Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804381 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804390 Share on other sites More sharing options...
Adam Posted April 8, 2009 Share Posted April 8, 2009 Where's "CreateUserLink($code)" come from? Can you post the code from that function as well.. Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804393 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804397 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 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! ??? Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804413 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804420 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 Sorry i have no idea, what logic your using here! Whats your input and expected output! Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804424 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804431 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 Wanna click solved then ? Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804438 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804453 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 Yep.. the first replace is replacing them all! Sorry i have no idea, what logic your using here! Whats your input and expected output! Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804459 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 I need it to act like bbcode, so that it replaces each one seperatly e.g. Here is a sentence where user1 and user2 are mentioned User1 and user2 need to be links which to there profiles,clearer? Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804461 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804465 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 That would be if the profile system uses usernames, but it doesn't sorry, it uses unique ID Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804467 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 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!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804477 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804482 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 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!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804488 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804500 Share on other sites More sharing options...
MadTechie Posted April 8, 2009 Share Posted April 8, 2009 if you put the function in a function then yes! Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804503 Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Author Share Posted April 8, 2009 Ahh ok, works now thanks, using preg_replace_callback i believe also allows inline bbcode? Quote Link to comment https://forums.phpfreaks.com/topic/152466-solved-regex-or-str_ireplace/#findComment-804511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.