Jump to content

Upper case a letter after a quote


The Little Guy

Recommended Posts

OK, I can not get this... I need to upper case the first letter in $str, but my word boundary is getting the single quote as part of the word boundary, along with bout. I want to just uppercase the b in the following code.

 

Note: The word to be upper case will not always be b

 

Here is the code:

<?php
$str = "how 'bout it?";
preg_replace("~'\b[a-z]{3,}\s~e","ucfirst('$0')",$str);
?>

Link to comment
https://forums.phpfreaks.com/topic/71912-upper-case-a-letter-after-a-quote/
Share on other sites

Use strpos, or a simpler regex, there won't always be a space after 'bout

 

ie: "that's all it's 'about".

 

You'll want to match, [space][single apostrophe][a-z]

 

I don't always want to match [space][single apostrophe][a-z] because sometimes its a the beginning of a sentence

 

ie: "'bout it" : http://viplyrics.com/artist/Young%20Joc/Unknown/'bout%20It

 

and sometimes I have a word like this: A'alnadda

 

In that case, I don't want the second "a" uppercase.

 

Here are all my current rules:

<?php
function editTxt($str){
$nedit = strtolower($str);
$find = array(
	"~\s'\b[a-z]\s~e",
	"~&~",
	"~\s\s+~",
	"~^(a|an|the)\s+(.+)~i");
$replace = array(
	"strtoupper('$0')",
	"and",
	" ",
	"$2, $1");
return preg_replace($find,$replace,$nedit);
}

Hmm, just a thought...couldn't you explode by the space, then deal with each word individually? If the first character of the word is an apostrophe, you'll want to make the next character uppercase?

 

<?php
$str = "how 'bout it?";
$words = explode(' ',$str);
foreach($words as $k =>$v){
if($v[0]== "'"){
	$words[$k][1] = strtoupper($v[1]);
}
}
$str = implode(' ',$words);
echo $str;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.