Rev 24 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 24 | Rev 25 | ||
---|---|---|---|
Line 416... | Line 416... | ||
416 | /** |
416 | /** |
417 | * Converts a string or an array of strings to an associative |
417 | * Converts a string or an array of strings to an associative |
418 | * bitmask array with the string(s) as key(s). |
418 | * bitmask array with the string(s) as key(s). |
419 | * |
419 | * |
420 | * Converts the argument to a bitmask array where each member's |
420 | * Converts the argument to a bitmask array where each member's |
421 | * value is a power of 2, so that arbitrary member values can be |
421 | * value is a power of 2, so that arbitrary member values can be |
422 | * added to an integer on which bitwise operations with the member |
422 | * added to an integer on which bitwise operations with the member |
423 | * value or a combination of member values are possible. |
- | |
424 | * |
- | |
425 | * @author (c) 2003 Thomas Lahn <SELFbug@PointedEars.de> |
- | |
426 | * @param $aArray |
- | |
427 | * String or array of strings to be converted. |
- | |
428 | */ |
- | |
429 | function getBitmaskArray($aArray) |
- | |
430 | { |
- | |
431 | $a = array(); |
- | |
432 | - | ||
433 | if (is_array($aArray)) |
- | |
434 | { |
- | |
435 | for ($i = 0; $i < count($aArray); $i++) |
- | |
436 | { |
- | |
437 | $a[$aArray[$i]] = pow(2, $i); |
- | |
438 | } |
- | |
439 | } |
- | |
440 | else |
- | |
441 | $a[$aArray] = 1; |
- | |
442 | - | ||
443 | return $a; |
- | |
444 | } |
- | |
445 | - | ||
446 | /** |
- | |
447 | * Returns the contents of a file as if include() was used. |
- | |
448 | * |
- | |
449 | * @param string $filename Path of the file to retrieve |
- | |
450 | * @return string File contents |
- | |
451 | */ |
- | |
452 | function get_include_content($filename) |
- | |
453 | { |
- | |
454 | if (is_file($filename)) |
- | |
455 | { |
- | |
456 | ob_start(); |
- | |
457 | include $filename; |
- | |
458 | $contents = ob_get_contents(); |
- | |
459 | ob_end_clean(); |
- | |
460 | return $contents; |
- | |
461 | } |
- | |
462 | - | ||
463 | return ''; |
- | |
464 | } |
- | |
465 | - | ||
466 | /** |
- | |
467 | * Replaces each group of expressions in a string with the same |
- | |
468 | * corresponding string. |
- | |
469 | * |
- | |
470 | * @param Array[Array[string] | string, string] $map |
- | |
471 | * @param string $subject |
- | |
472 | * @return string |
- | |
473 | * A copy of $subject with the provided mapping applied. |
- | |
474 | */ |
- | |
475 | function preg_replace_group($map = array(), $subject = '') |
- | |
476 | { |
- | |
477 | if ($subject) |
- | |
478 | { |
- | |
479 | for ($i = 0, $len = count($map); $i < $len; $i++) |
- | |
480 | { |
- | |
481 | $subject = preg_replace($map[$i][0], $map[$i][1], $subject); |
- | |
482 | } |
- | |
483 | } |
- | |
484 | - | ||
485 | return $subject; |
- | |
486 | } |
- | |
487 | - | ||
488 | /** |
- | |
489 | * Randomly encodes a string of characters. |
- | |
490 | * |
- | |
491 | * @param string $s |
- | |
492 | * String to be encoded |
- | |
493 | * @param string $format = 'sgml' |
- | |
494 | * Encoding format. Currently only SGML-based encoding of |
- | |
495 | * ASCII characters with character references is supported. |
- | |
496 | * @return string |
- | |
497 | */ |
- | |
498 | function randomEsc($s = '', $format = 'sgml') |
- | |
499 | { |
- | |
500 | $f = function_exists('mt_rand') ? 'mt_rand' : 'rand'; |
- | |
501 | - | ||
502 | return preg_replace_callback('/[\\x00-\\x7F]/', |
- | |
503 | create_function('$m', "return $f(0, 1)" . '? $m[0] : "&#" . ord($m[0]) . ";";'), |
- | |
504 | $s); |
- | |
505 | } |
- | |
506 | - | ||
507 | /** |
- | |
508 | * Reduces sequences of two or more consecutive white-space characters |
- | |
509 | * in an input to a single space. |
- | |
510 | * |
- | |
511 | * @param string $s |
- | |
512 | * @return string |
- | |
513 | */ |
- | |
514 | function reduceWhitespace($s) |
- | |
515 | { |
- | |
516 | return preg_replace('/\s{2,}/', ' ', $s); |
- | |
517 | } |
- | |
518 | - | ||
519 | function debug($x) |
- | |
520 | { |
- | |
521 | echo '<pre>'; |
- | |
522 | - | ||
523 | // if (is_array($x)) |
- | |
524 | // { |
- | |
525 | // print_r($x); |
- | |
526 | // } |
- | |
527 | // else |
- | |
528 | // { |
- | |
529 | var_dump($x); |
- | |
530 | // } |
- | |
531 | - | ||
532 | echo '</pre>'; |
- | |
533 | } |
- | |
534 | 423 | * value or a combination of member values are possible. |
|
- | 424 | * |
|
- | 425 | * @author (c) 2003 Thomas Lahn <SELFbug@PointedEars.de> |
|
- | 426 | * @param $aArray |
|
- | 427 | * String or array of strings to be converted. |
|
- | 428 | */ |
|
- | 429 | function getBitmaskArray($aArray) |
|
- | 430 | { |
|
- | 431 | $a = array(); |
|
- | 432 | ||
- | 433 | if (is_array($aArray)) |
|
- | 434 | { |
|
- | 435 | for ($i = 0; $i < count($aArray); $i++) |
|
- | 436 | { |
|
- | 437 | $a[$aArray[$i]] = pow(2, $i); |
|
- | 438 | } |
|
- | 439 | } |
|
- | 440 | else |
|
- | 441 | $a[$aArray] = 1; |
|
- | 442 | ||
- | 443 | return $a; |
|
- | 444 | } |
|
- | 445 | ||
- | 446 | /** |
|
- | 447 | * Returns the contents of a file as if include() was used. |
|
- | 448 | * |
|
- | 449 | * @param string $filename Path of the file to retrieve |
|
- | 450 | * @return string File contents |
|
- | 451 | */ |
|
- | 452 | function get_include_content($filename) |
|
- | 453 | { |
|
- | 454 | if (is_file($filename)) |
|
- | 455 | { |
|
- | 456 | ob_start(); |
|
- | 457 | include $filename; |
|
- | 458 | $contents = ob_get_contents(); |
|
- | 459 | ob_end_clean(); |
|
- | 460 | return $contents; |
|
- | 461 | } |
|
- | 462 | ||
- | 463 | return ''; |
|
- | 464 | } |
|
- | 465 | ||
- | 466 | /** |
|
- | 467 | * Replaces each group of expressions in a string with the same |
|
- | 468 | * corresponding string. |
|
- | 469 | * |
|
- | 470 | * @param Array[Array[string] | string, string] $map |
|
- | 471 | * @param string $subject |
|
- | 472 | * @return string |
|
- | 473 | * A copy of $subject with the provided mapping applied. |
|
- | 474 | */ |
|
- | 475 | function preg_replace_group($map = array(), $subject = '') |
|
- | 476 | { |
|
- | 477 | if ($subject) |
|
- | 478 | { |
|
- | 479 | for ($i = 0, $len = count($map); $i < $len; $i++) |
|
- | 480 | { |
|
- | 481 | $subject = preg_replace($map[$i][0], $map[$i][1], $subject); |
|
- | 482 | } |
|
- | 483 | } |
|
- | 484 | ||
- | 485 | return $subject; |
|
- | 486 | } |
|
- | 487 | ||
- | 488 | /** |
|
- | 489 | * Randomly encodes a string of characters. |
|
- | 490 | * |
|
- | 491 | * @param string $s |
|
- | 492 | * String to be encoded |
|
- | 493 | * @param string $format = 'sgml' |
|
- | 494 | * Encoding format. Currently only SGML-based encoding of |
|
- | 495 | * ASCII characters with character references is supported. |
|
- | 496 | * @return string |
|
- | 497 | */ |
|
- | 498 | function randomEsc($s = '', $format = 'sgml') |
|
- | 499 | { |
|
- | 500 | $f = function_exists('mt_rand') ? 'mt_rand' : 'rand'; |
|
- | 501 | ||
- | 502 | return preg_replace_callback('/[\\x00-\\x7F]/', |
|
- | 503 | create_function('$m', "return $f(0, 1)" . '? $m[0] : "&#" . ord($m[0]) . ";";'), |
|
- | 504 | $s); |
|
- | 505 | } |
|
- | 506 | ||
- | 507 | /** |
|
- | 508 | * Reduces sequences of two or more consecutive white-space characters |
|
- | 509 | * in an input to a single space. |
|
- | 510 | * |
|
- | 511 | * @param string $s |
|
- | 512 | * @return string |
|
- | 513 | */ |
|
- | 514 | function reduceWhitespace($s) |
|
- | 515 | { |
|
- | 516 | return preg_replace('/\s{2,}/', ' ', $s); |
|
- | 517 | } |
|
- | 518 | ||
- | 519 | function debug($x) |
|
- | 520 | { |
|
- | 521 | echo '<pre>'; |
|
- | 522 | ||
- | 523 | // if (is_array($x)) |
|
- | 524 | // { |
|
- | 525 | // print_r($x); |
|
- | 526 | // } |
|
- | 527 | // else |
|
- | 528 | // { |
|
- | 529 | var_dump($x); |
|
- | 530 | // } |
|
- | 531 | ||
- | 532 | echo '</pre>'; |
|
- | 533 | } |
|
- | 534 |