Jump to content


Capitalise SOME Letters [PHP]


6 replies to this topic

#1 Jackdawhouse

    Young Padawan

  • Members
  • Pip
  • 11 posts

Posted 24 July 2008 - 05:53 PM

Hello there.

I'm sure it's simpe but I can't think of a way of just capitalising four letters in a single string?

Example:

<?php

// some function

$name = "peter is not cool";

print function($name); // output: PETEr is not cool

?>

Any ideas?

Thank you.

Edited by Jackdawhouse, 24 July 2008 - 05:53 PM.


#2 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 24 July 2008 - 09:00 PM

php.net.ucwords()

example:

<?php

$txt = "hello world";

echo ucwords($txt);

/* output is Hello World */

?>

Edited by bigdfbc2008, 24 July 2008 - 09:01 PM.


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 July 2008 - 12:15 AM

Close, but no. He wants specifically the first four letters of a generic string. For that, there is no native function, you have to hack it.

substr() and strtoupper() would be the functions i would look at.

Edited by rc69, 25 July 2008 - 12:15 AM.


#4 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 25 July 2008 - 12:20 AM

You could always do it from an array, which in this case would be kind of hard but for example...

<?php

$search = array(
'puppy'
);

$replace = array(
'DOG'
);

$val = str_replace($search, $replace, $val);

echo $val;

?>

but just a thought that would get annoying to me lol

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 July 2008 - 12:27 AM

Unfortunately no again. A generic string can start with anything. To make an array out of all of the possible 4 letter/number/character combinations would easily exceed most finite memory limits.

If you wanted to use an array for this, then you would use str_split() with a length of 4. strtoupper() the first element in the array, then implode() the array. Thinking through how to make that work would be easier than the previous method, but depending on the intended use of this, the first method would probably be more efficient.

#6 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 25 July 2008 - 01:04 AM

View Postrc69, on Jul 25 2008, 01:27 AM, said:

Unfortunately no again. A generic string can start with anything. To make an array out of all of the possible 4 letter/number/character combinations would easily exceed most finite memory limits.

If you wanted to use an array for this, then you would use str_split() with a length of 4. strtoupper() the first element in the array, then implode() the array. Thinking through how to make that work would be easier than the previous method, but depending on the intended use of this, the first method would probably be more efficient.

lol listen to rc69 he knows way more than I do lol Not being sarcastic either.

#7 Tyson D

    Young Padawan

  • Members
  • Pip
  • 85 posts
  • Gender:Male
  • Location:Canada

Posted 25 July 2008 - 10:35 AM

Hey,
If you want something kind of reusable...

 <?php
 function uppercaseSubstring($str, $startpos, $length) {
	 $front = substr($str, 0, $startpos);
	 $end = substr($str, $startpos + $length, strlen($str) - ($startpos + $length)); 
	 $upper = strtoupper(substr($str, $startpos, $length));
 
	 return $front . $upper . $end;
 }
 ?>

So this just will uppercase a substring in a string, you can do it for any substring...

 $msg = "peter is cool";
  echo uppercaseSubstring($msg, 0, 4);

The above would print: PETEr is cool





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users