|
użytkowników online: 65
|
OPINIE UŻYTKOWNIKÓW
|
Porady zamieszczone tutaj przez Darka są pomocne w wielu chwilach. Wielokrotnie tworząc jakiś złożony serwis korzystam z tych porad. Można by tworzyć samemu te skrypty, ale tak naprawdę czy nie lepiej jest wziąć skrypt z tej strony i zmodyfikowac go dla swoich potrzeb? Wprawdzie możemy taki skrypt napisać sami, ale po co, skoro stracimy czas na coś, co ktoś juz napisał, przetestował i może zagwarantować, że działa poprawnie. Któryś raz z rzędu opłacam abonament i nie raz jeszcze opłacę. Kawał dobrej roboty i ogrom wiedzy w jednym miejscu.
Piotr Karamański Design Studio
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
ucwords (PHP 3 >= 3.0.3, PHP 4, PHP 5) ucwords --
Uppercase the first character of each word in a string
Descriptionstring ucwords ( string str )
Returns a string with the first character of each word in
str capitalized, if that character is
alphabetic.
The definition of a word is any string of characters
that is immediately after a whitespace (These are:
space, form-feed, newline, carriage return, horizontal tab,
and vertical tab).
Przykład 1. ucwords() example |
<?php
$foo = 'hello world!';
$foo = ucwords($foo); $bar = 'HELLO WORLD!';
$bar = ucwords($bar); $bar = ucwords(strtolower($bar)); ?>
|
|
Notatka: Ta funkcja jest bezpieczna dla danych
binarnych.
See also strtoupper(),
strtolower() and ucfirst().
User Contributed Notes24-Dec-2005 04:34
"ieure at php dot net", your idea is pure poetry!
The function below will standardize the capitalization on people's names and the titles of reports and essays . You may need to adapt the lists in "$all_uppercase" and "$all_lowercase" to suit the data that you are working with.
function my_ucwords($str, $is_name=false) {
// exceptions to standard case conversion
if ($is_name) {
$all_uppercase = '';
$all_lowercase = 'De La|De Las|Der|Van De|Van Der|Vit De|Von|Or|And';
} else {
// addresses, essay titles ... and anything else
$all_uppercase = 'Po|Rr|Se|Sw|Ne|Nw';
$all_lowercase = 'A|And|As|By|In|Of|Or|To';
}
$prefixes = 'Mc';
$suffixes = "'S";
// captialize all first letters
$str = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', strtolower(trim($str)));
if ($all_uppercase) {
// capitalize acronymns and initialisms e.g. PHP
$str = preg_replace("/\\b($all_uppercase)\\b/e", 'strtoupper("$1")', $str);
}
if ($all_lowercase) {
// decapitalize short words e.g. and
if ($is_name) {
// all occurences will be changed to lowercase
$str = preg_replace("/\\b($all_lowercase)\\b/e", 'strtolower("$1")', $str);
} else {
// first and last word will not be changed to lower case (i.e. titles)
$str = preg_replace("/(?<=\\W)($all_lowercase)(?=\\W)/e", 'strtolower("$1")', $str);
}
}
if ($prefixes) {
// capitalize letter after certain name prefixes e.g 'Mc'
$str = preg_replace("/\\b($prefixes)(\\w)/e", '"$1".strtoupper("$2")', $str);
}
if ($suffixes) {
// decapitalize certain word suffixes e.g. 's
$str = preg_replace("/(\\w)($suffixes)\\b/e", '"$1".strtolower("$2")', $str);
}
return $str;
}
// A name example
print my_ucwords("MARIE-LOU VAN DER PLANCK-ST.JOHN", true);
// Output: Marie-Lou van der Planc-St.John
// A title example
print my_ucwords("to be or not to be");
// Output: "To Be or Not to Be"
ieure at php dot net
04-Dec-2005 11:57
Whoa guys, tone things down a bit here. No need to loop and implode. This is a one-line solution:
function ucsmart($text)
{
return preg_replace('/([^a-z]|^)([a-z])/e', '"$1".strtoupper("$2")',
strtolower($text));
}
igua's code adds a backslash in front of the first single quote for me. This doesn't alter the content in any way other than changing case.
gothicbunny at hotmail dot com
09-Nov-2005 01:16
Here is a simple, yet winded, opposite to ucwords.
<?php
function lcwords($string)
{
$a = 0;
$string_new = array();
$string_exp = explode(" ",$string);
foreach($string_exp as $astring)
{
for($a=0;$a<strlen($astring);$a++)
{
if(preg_match("'\w'",$astring[$a]))
{
$astring[$a] = strtolower($astring[$a]);
break;
}
}
$string_new[] = $astring;
}
return implode(" ",$string_new);
}
?>
Of course a simplier way would be to use a callback, but I like working with long code :)
21-Oct-2005 01:14
Here's a piece that allows you to use the contents of a directory.. capitalizes the words and make links.. this particular example splits file names at _ and only selects file with .htm extensions (thought you could use any extension and call it using include() or soom such)
ie my_file_name.htm will produce
<a href="my_file_name.htm">My File Name</a>
<?php
$path = "/home/path/to/your/directory";
$mydir = dir($path);
while(($file = $mydir->read()) !== false) {
if(substr($file, -4)=='.htm'){
$trans = array("_" => " ", ".htm" => ""); $newlist = strtr($file, $trans); echo "<a href=\"".$file."\">".ucwords($newlist)."</a><br>";
}
}
?>
Static Bit
18-Sep-2005 05:01
// programming/repair -> Programming/Repair
// mcdonald o'neil -> McDonand O'Neil
// art of street -> Art of Street
function NomeProprio($nome)
{
//two space to one
$nome = str_replace(" ", " ", $nome);
$nome = str_replace(" ", " ", $nome);
$nome = str_replace(" ", " ", $nome);
$intervalo = 1;
for ($i=0; $i < strlen($nome); $i++)
{
$letra = substr($nome,$i,1);
if (((ord($letra) > 64) && (ord($letra) < 123)) || ((ord($letra) > 48) && (ord($letra) < 58)))
{
$checa_palavra = substr($nome, $i - 2, 2);
if (!strcasecmp($checa_palavra, 'Mc') || !strcasecmp($checa_palavra, "O'"))
{
$novonome .= strtoupper($letra);
}
elseif ($intervalo)
{
$novonome .= strtoupper($letra);
}
else
{
$novonome .= strtolower($letra);
}
$intervalo=0;
}
else
{
$novonome .= $letra;
$intervalo = 1;
}
}
$novonome = str_replace(" Of ", " of ", $novonome);
$novonome = str_replace(" Da ", " da ", $novonome);
$novonome = str_replace(" De ", " de ", $novonome);
$novonome = str_replace(" Do ", " do ", $novonome);
$novonome = str_replace(" E " , " e " , $novonome);
return $novonome;
}
radley25 at nospam dot spamcop dot net
05-Jul-2005 05:06
In response to joshuamallory at yahoo dot com:
Using CSS to fix a PHP fault is not the ideal way to solve a problem. CSS is browser dependent and can only be used when the data is presented in a web page. A better fix would be something like this:
<?php
function better_ucwords($string) {
$string = ucwords($string);
$string = preg_replace('#[\\/][a-z]#e', "strtoupper('$0')", $string);
return $string;
}
?>
igua no-spam at coveruniverse dot com
08-Mar-2005 01:30
The code posted by neil doesn't fully do what is wanted. Try adding some more question marks at the end and it will return a not wanted string.
Below code will uppercase all your words regardless of the delimiter.
<?php
$text = "What?No 'delimiters',shit \"happens\" here.this solves all problems???";
preg_match_all('/[A-Za-z]+|[^A-Za-z]+/', $text, $data);
for ($i = 0; $i < count($data[0]); $i++) {
$data[0][$i] = ucfirst($data[0][$i]);
}
$text = implode("", $data[0]);
print $text;
?>
arjini at gmail dot com
23-Jan-2005 09:20
Not so much ucwords() related as it is capital letter related. I often use camel casing (as do wikis), I needed a reason to reverse the camel casing.
function unCamelCase($str){
$bits = preg_split('/([A-Z])/',$str,false,PREG_SPLIT_DELIM_CAPTURE);
$a = array();
array_shift($bits);
for($i = 0; $i < count($bits); ++$i)
if($i%2)
$a[] = $bits[$i - 1].$bits[$i];
return $a;
}
print_r(unCamelCase('MyFancyCamelCasedWord'));
Array
(
[0] => My
[1] => Fancy
[2] => Camel
[3] => Cased
[4] => Word
)
joshuamallory at yahoo dot com
15-Nov-2004 05:08
If you want to format a string like...
<?php
$string = "computer programming/repair";
print ucwords($string);
?>
Output: Computer Programming/repair
Notice the word after the slash (Programming/repair) isn't capitalized. To fix this, use CSS...
<?php
$string = "computer programming/repair";
print '<p style="text-transform:capitalize">';
print ucwords($string);
print '<p>';
?>
babel - nospamplease - sympatico - ca
11-Feb-2004 05:26
Correction to the code of firewire at itsyourdomain dot com:
preg_replace_callback('/\b(\w)(\w+)?/',
create_function('$a',
'return strtoupper($a[1]) . ((sizeof($a) > 2 ) ?
strtolower($a[2]) : "");'),
'p.s.: hello.this is my string.');
Will work with punctuation as well as spaces.
deepdene at email dot com
10-Dec-2002 08:20
A function knowing about name case (i.e. caps on McDonald etc)
function name_case($name)
{
$newname = strtoupper($name[0]);
for ($i=1; $i < strlen($name); $i++)
{
$subed = substr($name, $i, 1);
if (((ord($subed) > 64) && (ord($subed) < 123)) ||
((ord($subed) > 48) && (ord($subed) < 58)))
{
$word_check = substr($name, $i - 2, 2);
if (!strcasecmp($word_check, 'Mc') || !strcasecmp($word_check, "O'"))
{
$newname .= strtoupper($subed);
}
else if ($break)
{
$newname .= strtoupper($subed);
}
else
{
$newname .= strtolower($subed);
}
$break=0;
}
else
{
// not a letter - a boundary
$newname .= $subed;
$break=1;
}
}
return $newname;
}
firewire at itsyourdomain dot com
20-Nov-2002 12:13
For those that want to capitalize based on a regular expression.
print preg_replace_callback('/(\s|^)[a-z]/', create_function('$a', 'return strtoupper($a[0]);'), 'hello this is my string');
This is a quick untested example.
anton at titov dot net
25-Sep-2002 07:56
for those, who not avoid regular expressions, solution of discussed problem:
$text=preg_replace('/(\W)(\w)/e', '"\\1".strtoupper("\\2")', ucfirst(strtolower($text)));
fille at fukt dot bth dot se
27-Aug-2002 05:04
[Editor's note: For details on the bug see
http://bugs.php.net/bug.php?id=14655]
This function has a bug, and while waiting for the bug fix, here is a work-around pice of code.
When using international letters, you will get into troubles with the ucwords() function.
Example:
$string="xxx
|