PDA

View Full Version : Regex a user name and append to a URL in template?


brdad
04-17-2011, 08:41 PM
This is my first post and I'm jumping in head first so please bear with me.

I want to format a user name removing unusable characters, and then append that user name to a URL for IRC chat. I'm not 100% sure I can do this in a template for a widget nor am I 100% sure how to code it. Any help would be appreciated. I know the code below is wrong, but it should be clear what I am trying to accomplish.

Thanks


<div class="cms_widget">
<div class="block">
<div class="cms_widget_header widget_header">
<h3>IRC Chat</h3>
</div>
<div class="cms_widget_content widget_content">

Dirty = $bbuserinfo[username]

$Clean = preg_replace('/^[0-9]+([^0-9])/', '\\1', preg_replace('/[^-a-zA-Z0-9_|^{}[\]\\\\]/', '', $Dirty));}

$Output = 'http://www.somesite.com/chat&user='$Clean


</div>
</div>
</div>

Disasterpiece
04-19-2011, 07:55 AM
$cn = preg_replace('/^[0-9]+([^0-9])/', '\\1', preg_replace('/[^-a-zA-Z0-9_|^{}[\]\\\\]/', '', $bbuserinfo["username"]));

$output .= '<a href="http://www.somesite.com/chat&user='.$cn.'">Join Chat!</a>';


try this

no guarantee on that regex operation tough, looks unnecessarily complicated.

brdad
04-19-2011, 10:26 AM
Thanks, I think I got it. Part of my problem was trying to do the php in a template instead of a direct exec widget itself.

I did have to split the regex up into two lines. One part has to remove unwanted characters, the other has to remove leading digits, or I have now changed it to add an underscore before leading digits (Still not fully proven, however!)

$dirty = vB::$vbulletin->userinfo['username'];
$clean = preg_replace('/[^-a-zA-Z0-9_|^{}[\]\\\\]/','',$dirty);
$cleaner = preg_replace('/^([0-9]+)([^0-9])/', '_\\1\\2',$clean);

ecrist
04-22-2011, 02:21 AM
For what it's worth, you could do this:

$clean = preg_replace(
array('/[^-a-zA-Z0-9_|^{}[\]\\\\]/','/^([0-9]+)([^0-9])/', '_\\1\\2'),
array('', '_\\1\\2'),
vB::$vbulletin->userinfo['username']
);




Sorry, it seems the PHP tags don't want to size the box properly above.