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
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);
}

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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