Studyon Minte9.com
ZCE 5.3

Study

Formatting



	// htmlentities(all); htmlspecialchars(&,", ', <, >)
	// ord(A) = 65 | chr(65) = A
	// wordwrap(12345 78, 7, #) = 12345#78 | [, cut=false] ignore width
	// number_format(100000, 3, ",", " ") = 100 000,000 | NOT local-aware
	// str_pad(333, 6, 0, STR_PAD_LEFT) = 000333
	// printf(Hello %s %s, A, B) = Hello A B | Output | printf(a)=a
	// a = sprintf() | String
	// fprintf($fd, %s-%s-%s, y, m, d) | String Length
	// vprintf(%s %d, array(a,1)) | echo = a 13 !
	// vsprintf(str, format, array()) | String
	// sscanf() | list(serial) = sscanf(SN/111, SN%d) | serial = 111


htmlentities() - convert special chars to entities

<?php echo htmlentities("A 'quote' is <b>bold</b>%"); // A 'quote' is &lt;b&gt;bold&lt;/b&gt; echo htmlentities('<Il etait une fois un etre>'); // etait with tilde // &lt;Il &Atilde;&copy;tait une fois un &Atilde;&ordf;tre&gt;
  htmlspecialchars() - convert html chars to entities
<?php echo htmlspecialchars("A 'quote' is <b>bold</b>%"); // A 'quote' is &lt;b&gt;bold&lt;/b&gt;% echo htmlspecialchars('<Il etait une fois un etre>'); // &lt;Il etait une fois un etre&gt;
  ord() - Return ASCII value of character
<?php echo ord("A"); // 65
  chr() - Return character by ASCII code
<?php echo chr(65); // A
  wordwrap()
<?php // wordwrap(string, $width=75, $break=|n) [, cut=false] // Wraps a string (if strlen <= width) echo wordwrap("12345 78", 7); // StrLength: 8; Maximum not to wrap: 7 // Output: 12345 n 78 echo wordwrap("12345 78", 7, "<br>"); // Replace default n with <br> // Output: 12345 <br> 78 echo wordwrap("12345678 abc", 7, "<br>", true); // 8 < 7 cut anyway // Output: 1234567 <br> 8 <br> abc
  number_format() - is not locale-aware
<?php echo number_format("100000.698"); // 100,001 // returns a rounded number echo number_format("100000.698", 3, ",", " "); // 100 000,698
  str_pad() - Pad a string to a certain length with another string
<?php echo str_pad("100", 6, "0"); // Output: 100000 echo str_pad("333", 6, "0", STR_PAD_LEFT); // Output: 000333
  money_format()
<?php // is locale-aware // not available on Windows, as well as on some variants of UNIX setlocale(LC_MONETARY, "en_US"); echo money_format('%.2n', "100000.698"); // $100,000.70 setlocale(LC_MONETARY, "ro_RO"); echo money_format('%.2n', "100000.698"); // Lei 100.000,70
  If we change our formatting to use the i conversion character, money_format() will produce its output using the international notation.
<?php setlocale(LC_MONETARY, "en_US"); echo money_format('%.2i', "100000.698"); // USD 100,000.70
  In addition, you should keep in mind that the default rounding rules change from locale to locale. For example, US currency values are regularly expressed as dollars and cents, while Japanese currency values are represented as integers.
<?php setlocale(LC_MONETARY, "en_US"); echo money_format('%i', "100000.698"); // USD 100,000.70 setlocale(LC_MONETARY, "ja_JA"); echo money_format('%i', "100000.698"); // JPY 100,001
  printf()
<?php printf ("The string is %s %s", "abc", "def"); // The string is abc def
  sprint()
<?php echo $a = sprintf("The number is %d", "123"); // The number is 123
  vsprintf()
<?php echo vsprintf("The string is %s %s", array("abc", "def")); // The string is abc def // Operates as sprintf() but accepts an array of arguments,
  fprintf()
<?php // write a formatted string to a stream if ($fd = fopen('date.txt', 'w')) { fprintf($fd, "%s-%s-%s", $y, $m, $d); }
  sscanf()
<?php // instead of formatting output, it allows you to parse formatted input list($serial) = sscanf("SN/331122", "SN/%d"); echo $serial; // 331122 list($month, $day, $year) = sscanf("January 01 2012", "%s %d %d"); echo "$month - $day - $year"; // January - 1 - 2012