以下是一个简单的PHP库文件实例,名为`lib.php`,其中包含了几个常用的函数,用于处理字符串、日期和数组等。

```php

实例lib.php,实例lib.php:PHP库文件编写实例详解  第1张

// lib.php

// 字符串处理函数

function truncateString($string, $length = 100) {

if (strlen($string) > $length) {

return substr($string, 0, $length) . '...';

}

return $string;

}

// 日期处理函数

function formatDate($date) {

return date('Y-m-d H:i:s', strtotime($date));

}

// 数组处理函数

function arrayToXml($data, $rootElement = 'root') {

$xml = new SimpleXMLElement($rootElement);

foreach ($data as $key => $value) {

if (is_array($value)) {

$subnode = $xml->addChild($key);

arrayToXml($value, $subnode);

} else {

$xml->addChild("