Jump to content

Problem with capitalization


Go to solution Solved by Barand,

Recommended Posts

Hello, I have some titles in which I am trying to capitalize the first letter of each word and I am running into a problem implementing it. I think it is because of the previous formatting with the pre_replace or I am just not doing something right. I am using the $str = ucwords($str) but it isn't working correctly. What am I doing wrong?

 

$WXSIMtitles[$i] = ucwords($WXSIMtitles[$i]);
 $WXSIMtitles[$i] = preg_replace('! (\S+)$!',"<br />\\1",get_lang($WXSIMday[$i]));
 if (! preg_match('!<br />!',$WXSIMtitles[$i])) {
   '<strong>' . $WXSIMtitles[$i] .= '<br />';  // add line break to 'short' day titles
 }

The first letter is capitalized but the first letter in the second word isn't. I.E. Afternoon & Night. 

 

b3b1.png

 

-Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/287789-problem-with-capitalization/
Share on other sites

Are these lines in the right order?

$WXSIMtitles[$i] = ucwords($WXSIMtitles[$i]);
 $WXSIMtitles[$i] = preg_replace('! (\S+)$!',"<br />\\1",get_lang($WXSIMday[$i]));

Shouldn't the first line be after the secound?

 

Also is the preg_replace use to convert newlines to <br />. If thats the case you could use nl2br instead

I have tried it in different orders and nether worked. I didn't write this script, I am just trying to make a small modification. I would agree nl2br would be a better option but not sure how to implement that with how it is currently written and don't really want to make a bunch of change if I don't need to just for one small modification. 

 

Here is how the variable is displayed. 

$WXSIMicons[$i] = "<strong>$WXSIMtitles[$i]</strong><br />
   <img src=\"$iconDir$WXSIMicon[$i]\" alt=\"".strip_tags($WXSIMcond[$i])."\"  title=\"".
   strip_tags($WXSIMcond[$i]) ."\" /><br />
   $WXSIMcond[$i]";

Not quite sure how to format this correctly to use the ucfirst. 

I am completely puzzled. I am not sure what is causing this but this works. 

 

It will make all characters upper case but, I only need the first character of each word upper case

$WXSIMtitles[$i] = strtoupper($WXSIMtitles[$i]);
So when I try this, it doesn't work, only the first character is upper case. 
 
$WXSIMtitles[$i] = ucwords(strtolower($WXSIMtitles[$i]));

This works....

 

 
  $WXSIMtitles[$i] = strtoupper($WXSIMtitles[$i]);
 //$WXSIMtitles[$i] = ucwords(strtolower($WXSIMtitles[$i]));
   $WXSIMicons[$i] = "<strong>$WXSIMtitles[$i]</strong><br /><img src=\"$iconDir$WXSIMicon[$i]\" alt=\"".strip_tags($WXSIMcond[$i])."\"  title=\"".
   strip_tags($WXSIMcond[$i]) ."\" /><br />
   $WXSIMcond[$i]";

This does not...

 

   
//$WXSIMtitles[$i] = strtoupper($WXSIMtitles[$i]);
   $WXSIMtitles[$i] = ucwords(strtolower($WXSIMtitles[$i]));
   $WXSIMicons[$i] = "<strong>$WXSIMtitles[$i]</strong><br /><img src=\"$iconDir$WXSIMicon[$i]\" alt=\"".strip_tags($WXSIMcond[$i])."\"  title=\"".
   strip_tags($WXSIMcond[$i]) ."\" /><br />
   $WXSIMcond[$i]";

What would be causing this? 

What is the output of this

printf('<pre>%s</pre>', print_r(htmlentities($WXSIMtitles[$i]), true));

There is most likely some stray characters between the two words which maybe causing the issue with only the first word to be capitalised

Edited by Ch0cu3r

What is the output of this

printf('<pre>%s</pre>', print_r(htmlentities($WXSIMtitles[$i]), true));

There is most likely some stray characters between the two words which maybe causing the issue with only the first word to be capitalised

 

I never even thought of that but the logic behind it makes sense. So I have attached a screenshot and there doesn't appear to be any special characters preventing it which now really has me scratching my head but I think the code is right. I am puzzled. I thought maybe I wasn't using the ucwords right so I tried it with the stroupper and it worked. That's when I really was puzzled because the ucwords should work unless it is some how rendering the line break as characters maybe but if then shouldn't it read it as the next line?

 

EDIT: Well scratch that idea. I removed the break and it still doesn't work even with that removed. 

 

w9mr.png

Edited by Texan78
  • Solution

The character immediately before the second word needs to be whitespace for ucwords() to work

$str = "this\nafternoon";
echo nl2br(ucwords($str));

=>

This

Afternoon

$str = "this<br>afternoon";
echo ucwords($str);

=>

This

afternoon

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.