PDA

View Full Version : Apostrophes


Boofo
08-28-2002, 10:36 AM
Can anyone please help me with the following problem?

I have a user whose name ends with an apostrophe (i.e Bonet'). I want to make the name with an s like this (for ownership) Bonet's instead of Bonet''s with 2 apostrophes. I am using $userinfo[username] to get the name and if I use an 's for ownership on all other names it appears fine. I need to have it not add an apostrophe after the name if one is already there, just the s, but still keep the 's for all other names. I know it sounds confusing. :) I'm not sure how to pull the information out of the username variable to do this. Can anybody help?

Scott MacVicar
08-28-2002, 11:27 AM
em you could use a regexp but simpliest way is

if(substr($userinfo['username'], -1) == '\\'') {
$userinfo['username'] .= 's';
} else {
$userinfo['username'] .= '\\'s';
}

Boofo
08-28-2002, 11:40 AM
Scott, that works perfect. As a matter of fact, it works too perfect. Is there a way I can use a variable after the users name (like $s) instead so I can just use it where I need it and not have it show up where I don't? :)

BTW: What is a regexp? Just curious. :)

Originally posted by PPN
em you could use a regexp but simplest way is

if(substr($userinfo['username'], -1) == '\\'') {
$userinfo['username'] .= 's';
} else {
$userinfo['username'] .= '\\'s';
}

Boofo
08-28-2002, 11:48 AM
Here's what I came up with. It seems to work. Is it right? And thank you, Scott. :)

if(substr($userinfo['username'], -1) == '\\'') {
$a= 's';
} else {
$a= '\\'s';
}

Admin
08-28-2002, 12:21 PM
<a href="http://php.fastmirror.com/manual/en/ref.pcre.php" target="_blank">http://php.fastmirror.com/manual/en/ref.pcre.php</a>

Scott MacVicar
08-28-2002, 12:42 PM
could use

if (preg_match ("(\\'$)", $userinfo['username'])) {
$a .= 's';
} else {
$a .= '\\'s';
}

Boofo
08-28-2002, 01:01 PM
Ok, one last question, please. And thank you for all of the help on this. I really appreciate it. :)

Is there a way to do it globally for any username variable (i.e. $bbuserinfo[username] and $userinfo[username] and $post[username]) instead of having to do it individually in each area and with each different username variable?

And thank you for the link, Chen. :) I will check it out.

Scott MacVicar
08-28-2002, 02:52 PM
you could just make a function

function doapostrophe($username) {
if (preg_match ("(\\'$)", $username)) {
$username .= 's';
} else {
$username .= '\\'s';
}
return $username;
}

and when you need to just use
$userinfo['username'] = doapostrophe($userinfo['username']);

something like that?

Boofo
08-28-2002, 02:57 PM
That sounds like what I am looking for. But would there be a way to make it with the $a variable so I could just add the $a where I needed it instead of having to do the doapostrophe all over the site? And I could put this anywhere in the functions.php and it would work site wide, right? Or do I need to put it a certain place in there?

Originally posted by PPN
you could just make a function

function doapostrophe($username) {
if (preg_match ("(\\'$)", $username)) {
$username .= 's';
} else {
$username .= '\\'s';
}
return $username;
}

and when you need to just use
$userinfo['username'] = doapostrophe($userinfo['username']);

something like that?

Scott MacVicar
08-28-2002, 08:12 PM
You could just use

$userinfo['usernames'] = doapostrophe($userinfo['username']);

and just user $userinfo['usernames'] where you want it with the s at the end.

Correct anywhere in functions.php

Boofo
08-29-2002, 12:09 AM
Is there a way to have this variable:

$a

check whether there is a

'
before it and if there is, just add an s and if there isn't, add an

's

That way, I can put it in any template where I need to and if the name (whether it be $bbuserinfo[username] or $post[username] or whatever else I use) ends in an apotrophe, it will only add an s and do the other if there is no apostrophe.

I'm hoping to be able to use this anywhere at any time.

Scott MacVicar
08-29-2002, 01:15 AM
Would involve a good bit of modification, i'll try to find all the times that the username is loaded and add the code which adds a usernames key to the array so instead of using $userinfo['username']$a you can use $userinfo['usernames']

Boofo
08-29-2002, 01:25 AM
I really appreciate this, Scott, thank you! :)

How would that affect the places I have $bbuserinfo[usernames]and $post[username]? Would I just need to change all o0f those instances everywhere to $userinfo['username']? I want to be able to do this in the templates instead of having to go through all of the php files to find where I have those different variables. Because I don't need the 's or s after every instance of the username on the board. Just select areas. I started to modify each place last night and I gave up after getting confused with all of the different ways I call the username and the different places. :) That is why I thought using the $a would be easier because then I could just add it as I need it in the template and not have to re-write all of the code for each variable in the php files.

Originally posted by PPN
Would involve a good bit of modification, i'll try to find all the times that the username is loaded and add the code which adds a usernames key to the array so instead of using $userinfo['username']$a you can use $userinfo['usernames']

Scott MacVicar
08-29-2002, 08:59 AM
You will have two variables
$bbuserinfo['username'] and $bbuserinfo['usernames']

one without 's and one with so you just edit the template to use one or the other.

Boofo
08-29-2002, 09:05 AM
Great! That's even better than the $a idea. :) Will I be able to use those globally? I won't need a separate post[username] and userinfo[username] anymore then? That would be fantastic. :) Looking forward to it, my friend. :)

Originally posted by PPN
You will have two variables
$bbuserinfo['username'] and $bbuserinfo['usernames']

one without 's and one with so you just edit the template to use one or the other.

Scott MacVicar
08-29-2002, 09:29 AM
Yep just gotta modify a couple of functions where these arrays are derrived from

/admin/sessions.php for $bbuserinfo
verifyuser function for the other but someplaces might not get their array from any main functions, like getpostbit i believe.

Boofo
08-29-2002, 09:35 AM
The private messages, and the usercp should work with that though, don't you think? Along with the postbit and memberlist? If the getinfo is the only place I have to use something else to get the same results, I can live with that. :)