Texan78 Posted April 15, 2014 Share Posted April 15, 2014 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. -Thanks Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted April 15, 2014 Share Posted April 15, 2014 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 Quote Link to comment Share on other sites More sharing options...
Texan78 Posted April 15, 2014 Author Share Posted April 15, 2014 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. Quote Link to comment Share on other sites More sharing options...
Texan78 Posted April 16, 2014 Author Share Posted April 16, 2014 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? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted April 16, 2014 Share Posted April 16, 2014 (edited) 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 April 16, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Texan78 Posted April 16, 2014 Author Share Posted April 16, 2014 (edited) 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. Edited April 16, 2014 by Texan78 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted April 16, 2014 Solution Share Posted April 16, 2014 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 Quote Link to comment Share on other sites More sharing options...
Texan78 Posted April 17, 2014 Author Share Posted April 17, 2014 That did the trick! I replace the <br /> in the preg_replace with the \n and presto! All is great in the universe again. Thank you and you too Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.