Having trouble getting some code to auto-capitalize words properly

Seems like you could just change your preg_split to “/([^a-zA-Z0-9’])/” so that it will match the numbers as well.

Thank you for the reply Matt!

I ended up making the change and I have this code now:

[php]if (is_subclass_of($this, ‘vB_DataManager_ThreadPost’) && is_array($this->validfields[‘title’])) {

$this->validfields[‘title’][VF_CODE] = ’
global $exclude_words, $capital_words, $max_length;
$exclude_words = array(“a”,“an”,“and”,“at”,“but”,“by”,“for”,“in”,“nor”,“of”,“on”,“or”,“so”,“the”,“to”,“up”,“yet”); // Exclude analyzing these words
$capital_words = array(“brb”,“lol”,“imho”); // Capital exclusives (leave in lowercase in array)
$max_length = 5; // Maximum word length, anything over is case-lowered
$ending_punc = array(".","!","?");
$retval = $dm->verify_title($data);
$output = array();
$start = 1;
$words = preg_split("/([^a-zA-Z0-9\x27])/", $data, -1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

foreach($words as $i=>$word)
{
$lcWord = strtolower($word);
$ucWord = strtoupper($word);
if(in_array($lcWord,$capital_words)) $output[$i] = strtoupper($word); // Word was in capital words array
elseif($ucWord==$word) // Word was entered all caps and not in capital words array
{
if(strlen($word)>$max_length) $output[$i] = ucWords($lcWord);
else $output[$i] = $ucWord;
}
elseif($start == 1) $output[$i] = ucWords($lcWord);
elseif(in_array($lcWord,$exclude_words)) $output[$i] = $lcWord;
else $output[$i] = ucWords($lcWord);
if(in_array($word,$ending_punc)) $start = 1;
else if($word != " ") $start = 0;
}

$data = implode($output);

return $retval;
';
}[/php]

The only problem I am still having is not getting double quotes to properly get parsed. They continue to show up as ".

If my input is: “Hello” my output is: “Hello”

Would you happen to have any idea as to why this is happening?

Thank you so much for your time! :slight_smile:

I wasn’t able to duplicate this problem with your code. I converted “hello” to “Hello” without converting quotes to "

Ah, yeah, this issue is a sneaky one.

Malasho (jay) had figured out that it was actually happening due to something in the vBulletin script code but we were unable to come up with a way to prevent it from happening.

You’re right, when you just use the code as a standalone PHP script, it works, but for some reason when I use it inside my vBulletin script, it is messed up.

The confusing part is that if I don’t use this code, I am able to properly use double quotes on my forum, so there is something about this code that prevents that from happening. What that is, I don’t know. :frowning:

I’m not familiar with vbulletin but it seems they are using htmlentities() on the variable. What if you converted it back after being filtered by vbulleting using html_entity_decode()?

Thank you for responding Matt.

I’m lost as to exactly what I would need to do to make your suggestion happen. Is there something I add to the code I have or would I need to edit a vBulletin file directly?

Thank you.

Well, as I said I’m not familiar with vbulletin. I don’t know what this code does:

$retval = $dm->verify_title($data);

Perhaps something like $retval = html_entity_decode($dm->verify_title($data));

?

The function itself really doesn’t make any sense to me. I’m not sure if something is missing. You are manipulating $data but returning $retval. So what is $data?

This code runs in between a user inputting a thread title and then that being saved to the database I believe.

So in my crude understanding, $data is the thread title that the user entered, and then this code runs on it and returns $retval after processing it…I may be totally wrong about this though.

I tried changing the line to this: $retval = html_entity_decode($dm->verify_title($data));

but that ends in an error that no thread title was entered.

If you can send me the whole file (class?) I might be able to help. Otherwise malasho will hopefully come back and help :slight_smile:

Sure will send it right away. Give me 2 minutes. Thank you!

Here you go: http://pastebin.com/pB5vqnuH

Hm… I couldn’t find any reference to your code in that class.

Oh I’m sorry. My code actually just gets inserted as a “plugin” inside the vBulletin admin control panel. It hooks into the codebase and works somehow.

In this post (http://www.phphelp.com/forum/index.php?topic=18215.msg62473#msg62473) Malasho had me edit this file that I just pasted and then the code worked with double quotes and thats why I included this file because I figured it had something to do with it.

I am not sure what other file I would need to send but if you have any idea I would be glad to send it!

Thank you again.

Unfortunately I’m not familiar with vbulletin plugins. If I have time this weekend I will look in to it further since I’m always willing to learn new things :slight_smile:

Thank you! :stuck_out_tongue:

So M@tt and Malasho,

Wanted to thank you guys again for all your help and update you on this.

Building off of M@tt’s original suggestion of changing my preg_split regex, I went ahead and added x22 (the ascii code for quotation marks) and it works perfectly so far in my testing! :slight_smile:

Here is the final code I ended up with:

[php]if (is_subclass_of($this, ‘vB_DataManager_ThreadPost’) && is_array($this->validfields[‘title’])) {

$this->validfields[‘title’][VF_CODE] = ’
global $exclude_words, $capital_words, $max_length;
$exclude_words = array(“a”,“an”,“and”,“at”,“but”,“by”,“for”,“in”,“nor”,“of”,“on”,“or”,“so”,“the”,“to”,“up”,“yet”); // Exclude analyzing these words
$capital_words = array(“brb,“lol”); // Capital exclusives (leave in lowercase in array)
$max_length = 5; // Maximum word length, anything over is case-lowered
$ending_punc = array(”.","!","?");
$retval = $dm->verify_title($data);
$output = array();
$start = 1;
$words = preg_split("/([^a-zA-Z0-9\x27\x26\x22])/", $data, -1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

foreach($words as $i=>$word)
{
$lcWord = strtolower($word);
$ucWord = strtoupper($word);
if(in_array($lcWord,$capital_words)) $output[$i] = strtoupper($word); // Word was in capital words array
elseif($ucWord==$word) // Word was entered all caps and not in capital words array
{
if(strlen($word)>$max_length) $output[$i] = ucWords($lcWord);
else $output[$i] = $ucWord;
}
elseif($start == 1) $output[$i] = ucWords($lcWord);
elseif(in_array($lcWord,$exclude_words)) $output[$i] = $lcWord;
else $output[$i] = ucWords($lcWord);
if(in_array($word,$ending_punc)) $start = 1;
else if($word != " ") $start = 0;
}

$data = implode($output);

return $retval;
';
}[/php]

Is there any downside to me adding that in the regex or is this a great solution? Looking forward to your guys’ insights on it.

Thank you again!

I couldn’t duplicate the problem so I can’t say if that’s the best solution. If it works, I don’t see any problem with it. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service