5 Replies - 6456 Views - Last Post: 23 May 2010 - 09:06 PM Rate Topic: -----

#1 jason-pc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 61
  • Joined: 10-January 09

preg_replace but for UPPER and lower case

Posted 22 January 2009 - 10:35 AM

please can someone tell me what i can use instead of preg_replace

which does the same but for UPPER and lower case
Is This A Good Question/Topic? 0
  • +

Replies To: preg_replace but for UPPER and lower case

#2 rmccarter721  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 139
  • Joined: 10-September 07

Re: preg_replace but for UPPER and lower case

Posted 22 January 2009 - 10:39 AM

strtoupper

This post has been edited by Furnfield: 22 January 2009 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: preg_replace but for UPPER and lower case

Posted 22 January 2009 - 12:13 PM

^ Not a very thorough answer, so I'll indulge:

<?php
$str = "lalalalala";
$str = strtoupper($str);
echo $str; // Output: LALALALALA
$str = "LALALALALA";
$str = strtolower($str);
echo $str; // Output: lalalalala
?>



strtoupper(); and strtolower();

... will change the caps of your string according to your function.

As for PREG_REPLACE();, if you are trying to replace upper AND lower characters, your syntax will be as follows:

<?php
$str = "hello world";
$str = preg_replace('[a-z]', '[A-Z]', $str);
echo $str; // Output: HELLO WORLD
?>




As you can see, using preg_replace(); we CAN change the case of a string... using Regular Expressions. Obviously, to modify what characters are replaced (in this case, the whole alphabet) you just need to modify your REGEX patterns.


Hope this helps, let me know!

This post has been edited by pr4y: 22 January 2009 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: preg_replace but for UPPER and lower case

Posted 22 January 2009 - 01:30 PM

Another route for case insensitivity (an easier one) would be the following:

preg_replace("/\w+/", $subject, $matches);

$subject is what you're searching, of course, and $matches is optional. The regex above looks for any combination of word characters, case insensitive (this matches digits also!).

If you need one that does not match digits, use this one:

preg_replace("/[A-Za-z]+/", $subject, $matches);

Or this:

preg_replace("/(?i)[a-z]/", $subject, $matches);

All of the above will do what you want. It just depends on the approach you want to take :)
Was This Post Helpful? 0
  • +
  • -

#5 rmccarter721  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 139
  • Joined: 10-September 07

Re: preg_replace but for UPPER and lower case

Posted 22 January 2009 - 02:55 PM

Yea sorry, i had meant to try it out myself then edit my post to include a few examples, but i got called away and I am just getting home now..
Was This Post Helpful? 0
  • +
  • -

#6 Guest_MeLLZ*


Reputation:

Re: preg_replace but for UPPER and lower case

Posted 23 May 2010 - 09:06 PM

Ii work if you use
<?php
$str = "hello world";
$str = preg_replace('[a-z]', '[A-Z]', '$0');
echo $str;
?>
Was This Post Helpful? 0

Page 1 of 1