本文整理汇总了PHP中str_pad函数的典型用法代码示例。如果您正苦于以下问题:PHP str_pad函数的具体用法?PHP str_pad怎么用?PHP str_pad使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了str_pad函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _str_pad
/**
* utf8::str_pad
*
* @package Core
* @author Kohana Team
* @copyright (c) 2007 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
function _str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
if (utf8::is_ascii($str) and utf8::is_ascii($pad_str)) {
return str_pad($str, $final_str_length, $pad_str, $pad_type);
}
$str_length = utf8::strlen($str);
if ($final_str_length <= 0 or $final_str_length <= $str_length) {
return $str;
}
$pad_str_length = utf8::strlen($pad_str);
$pad_length = $final_str_length - $str_length;
if ($pad_type == STR_PAD_RIGHT) {
$repeat = ceil($pad_length / $pad_str_length);
return utf8::substr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
}
if ($pad_type == STR_PAD_LEFT) {
$repeat = ceil($pad_length / $pad_str_length);
return utf8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
}
if ($pad_type == STR_PAD_BOTH) {
$pad_length /= 2;
$pad_length_left = floor($pad_length);
$pad_length_right = ceil($pad_length);
$repeat_left = ceil($pad_length_left / $pad_str_length);
$repeat_right = ceil($pad_length_right / $pad_str_length);
$pad_left = utf8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
$pad_right = utf8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_left);
return $pad_left . $str . $pad_right;
}
trigger_error('utf8::str_pad: Unknown padding type (' . $type . ')', E_USER_ERROR);
}
示例2: createSymbolicLink
public static function createSymbolicLink($rootDir = null)
{
IS_MULTI_MODULES || exit('please set is_multi_modules => true');
$deper = Request::isCli() ? PHP_EOL : '<br />';
echo "{$deper}**************************create link start!*********************{$deper}";
echo '|' . str_pad('', 64, ' ', STR_PAD_BOTH) . '|';
is_null($rootDir) && ($rootDir = ROOT_PATH . DIRECTORY_SEPARATOR . 'web');
is_dir($rootDir) || mkdir($rootDir, true, 0700);
//modules_static_path_name
// 递归遍历目录
$dirIterator = new \DirectoryIterator(APP_MODULES_PATH);
foreach ($dirIterator as $file) {
if (!$file->isDot() && $file->isDir()) {
$resourceDir = $file->getPathName() . DIRECTORY_SEPARATOR . Config::get('modules_static_path_name');
if (is_dir($resourceDir)) {
$distDir = ROOT_PATH . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . $file->getFilename();
$cmd = Request::operatingSystem() ? "mklink /d {$distDir} {$resourceDir}" : "ln -s {$resourceDir} {$distDir}";
exec($cmd, $result);
$tip = "create link Application [{$file->getFilename()}] result : [" . (is_dir($distDir) ? 'true' : 'false') . "]";
$tip = str_pad($tip, 64, ' ', STR_PAD_BOTH);
print_r($deper . '|' . $tip . '|');
}
}
}
echo $deper . '|' . str_pad('', 64, ' ', STR_PAD_BOTH) . '|';
echo "{$deper}****************************create link end!**********************{$deper}";
}
示例3: setUp
public function setUp()
{
parent::setUp();
$webapp = Webapp::getInstance();
$webapp->registerPlugin('flickr', 'ExpandURLsPlugin');
//Add owner
$q = "INSERT INTO tu_owners SET id=1, full_name='ThinkUp J. User', email='me@example.com',\n is_activated=1, pwd='XXX', activation_code='8888'";
$this->testdb_helper->runSQL($q);
//Add instance_owner
$q = "INSERT INTO tu_owner_instances (owner_id, instance_id) VALUES (1, 1)";
$this->testdb_helper->runSQL($q);
//Insert test data into test table
$q = "INSERT INTO tu_users (user_id, user_name, full_name, avatar, last_updated) VALUES (13, 'ev',\n 'Ev Williams', 'avatar.jpg', '1/1/2005');";
$this->testdb_helper->runSQL($q);
//Make public
$q = "INSERT INTO tu_instances (id, network_user_id, network_username, is_public) VALUES (1, 13, 'ev', 1);";
$this->testdb_helper->runSQL($q);
//Add a bunch of posts
$counter = 0;
while ($counter < 40) {
$pseudo_minute = str_pad($counter, 2, "0", STR_PAD_LEFT);
$q = "INSERT INTO tu_posts (post_id, author_user_id, author_username, author_fullname, author_avatar,\n post_text, source, pub_date, reply_count_cache, retweet_count_cache) VALUES ({$counter}, 13, 'ev', \n 'Ev Williams', 'avatar.jpg', 'This is post {$counter}', 'web', '2006-01-01 00:{$pseudo_minute}:00', " . rand(0, 4) . ", 5);";
$this->testdb_helper->runSQL($q);
$counter++;
}
}
示例4: xml_pretty_printer
/**
* Takes xml as a string and returns it nicely indented
*
* @param string $xml The xml to beautify
* @param boolean $html_output If the xml should be formatted for display on an html page
* @return string The beautified xml
*/
function xml_pretty_printer($xml, $html_output = FALSE)
{
$xml_obj = new SimpleXMLElement($xml);
$xml_lines = explode("n", $xml_obj->asXML());
$indent_level = 0;
$new_xml_lines = array();
foreach ($xml_lines as $xml_line) {
if (preg_match('#(<[a-z0-9:-]+((s+[a-z0-9:-]+="[^"]+")*)?>.*<s*/s*[^>]+>)|(<[a-z0-9:-]+((s+[a-z0-9:-]+="[^"]+")*)?s*/s*>)#i', $xml_line)) {
$new_line = str_pad('', $indent_level * 4) . $xml_line;
$new_xml_lines[] = $new_line;
} elseif (preg_match('#<[a-z0-9:-]+((s+[a-z0-9:-]+="[^"]+")*)?>#i', $xml_line)) {
$new_line = str_pad('', $indent_level * 4) . $xml_line;
$indent_level++;
$new_xml_lines[] = $new_line;
} elseif (preg_match('#<s*/s*[^>/]+>#i', $xml_line)) {
$indent_level--;
if (trim($new_xml_lines[sizeof($new_xml_lines) - 1]) == trim(str_replace("/", "", $xml_line))) {
$new_xml_lines[sizeof($new_xml_lines) - 1] .= $xml_line;
} else {
$new_line = str_pad('', $indent_level * 4) . $xml_line;
$new_xml_lines[] = $new_line;
}
} else {
$new_line = str_pad('', $indent_level * 4) . $xml_line;
$new_xml_lines[] = $new_line;
}
}
$xml = join("n", $new_xml_lines);
return $html_output ? '<pre>' . htmlentities($xml) . '</pre>' : $xml;
}
示例5: composeId
/**
* create an auction id out of the shard and row id.
*/
public static function composeId($shard, $row_id)
{
if (strlen($shard) != 6 || !ctype_digit(strval($shard)) || !ctype_digit(strval($row_id))) {
return NULL;
}
return $shard . str_pad($row_id, 11, '0', STR_PAD_LEFT);
}
示例6: index
/**
* List download stats.
*
* @param bool|false $Offset
*/
public function index($Offset = false)
{
$this->permission('Garden.Settings.Manage');
$this->addSideMenu('vstats');
$this->addJsFile('jquery.gardenmorepager.js');
$this->title('Vanilla Stats');
$this->Form->Method = 'get';
$Offset = is_numeric($Offset) ? $Offset : 0;
$Limit = 19;
$this->StatsData = array();
$Offset--;
$Year = date('Y');
$Month = date('m');
$BaseDate = Gdn_Format::toTimestamp($Year . '-' . str_pad($Month, 2, '0', STR_PAD_LEFT) . '-01 00:00:00');
for ($i = $Offset; $i <= $Limit; ++$i) {
$String = "-{$i} month";
$this->StatsData[] = $this->_getStats(date("Y-m-d 00:00:00", strtotime($String, $BaseDate)));
}
$TotalRecords = count($this->StatsData);
// Build a pager
$PagerFactory = new Gdn_PagerFactory();
$this->Pager = $PagerFactory->getPager('MorePager', $this);
$this->Pager->MoreCode = 'More';
$this->Pager->LessCode = 'Previous';
$this->Pager->ClientID = 'Pager';
$this->Pager->Wrapper = '<tr %1$s><td colspan="6">%2$s</td></tr>';
$this->Pager->configure($Offset, $Limit, $TotalRecords, 'vstats/index/%1$s/');
// Deliver json data if necessary
if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
$this->setJson('LessRow', $this->Pager->toString('less'));
$this->setJson('MoreRow', $this->Pager->toString('more'));
}
$this->render();
}
示例7: configure
public function configure()
{
$status = array('Enabled' => __('Enabled'), 'Disabled' => __('Disabled'));
$idGenService = new IDGeneratorService();
$idGenService->setEntity(new Employee());
$empNumber = $idGenService->getNextID(false);
$employeeId = str_pad($empNumber, 4, '0');
$this->widgets = array('firstName' => new sfWidgetFormInputText(array(), array("class" => "formInputText", "maxlength" => 30)), 'middleName' => new sfWidgetFormInputText(array(), array("class" => "formInputText", "maxlength" => 30)), 'lastName' => new sfWidgetFormInputText(array(), array("class" => "formInputText", "maxlength" => 30)), 'empty' => new ohrmWidgetDiv(), 'fullNameLabel' => new ohrmWidgetDiv(), 'firstNameLabel' => new ohrmWidgetDiv(), 'middleNameLabel' => new ohrmWidgetDiv(), 'lastNameLabel' => new ohrmWidgetDiv(), 'employeeId' => new sfWidgetFormInputText(array(), array("class" => "formInputText", "maxlength" => 10, "colspan" => 3)), 'photofile' => new sfWidgetFormInputFileEditable(array('edit_mode' => false, 'with_delete' => false, 'file_src' => ''), array("class" => "duplexBox", "colspan" => 3)), 'chkLogin' => new sfWidgetFormInputCheckbox(array('value_attribute_value' => 1), array("style" => "vertical-align:top", "colspan" => 3)), 'lineSeperator' => new ohrmWidgetDiv(array(), array("colspan" => 3)), 'user_name' => new sfWidgetFormInputText(array(), array("class" => "formInputText", "maxlength" => 20)), 'status' => new sfWidgetFormSelect(array('choices' => $status), array("class" => "formInputText", "br" => true)), 'user_password' => new sfWidgetFormInputPassword(array(), array("class" => "formInputText passwordRequired", "maxlength" => 20)), 're_password' => new sfWidgetFormInputPassword(array(), array("class" => "formInputText passwordRequired", "maxlength" => 20, "br" => true)), 'empNumber' => new sfWidgetFormInputHidden());
$this->widgets['empNumber']->setDefault($empNumber);
$this->widgets['employeeId']->setDefault($employeeId);
if ($this->getOption('employeeId') != "") {
$this->widgets['employeeId']->setDefault($this->getOption('employeeId'));
}
$this->widgets['firstName']->setDefault($this->getOption('firstName'));
$this->widgets['middleName']->setDefault($this->getOption('middleName'));
$this->widgets['lastName']->setDefault($this->getOption('lastName'));
$this->widgets['chkLogin']->setDefault($this->getOption('chkLogin'));
$this->widgets['user_name']->setDefault($this->getOption('user_name'));
$this->widgets['user_password']->setDefault($this->getOption('user_password'));
$this->widgets['re_password']->setDefault($this->getOption('re_password'));
$this->widgets['status']->setDefault($this->getOption('status'));
$this->setWidgets($this->widgets);
$this->setValidators(array('photofile' => new sfValidatorFile(array('max_size' => 1000000, 'required' => false)), 'firstName' => new sfValidatorString(array('required' => true, 'max_length' => 30, 'trim' => true)), 'empNumber' => new sfValidatorString(array('required' => false)), 'lastName' => new sfValidatorString(array('required' => true, 'max_length' => 30, 'trim' => true)), 'middleName' => new sfValidatorString(array('required' => false, 'max_length' => 30, 'trim' => true)), 'employeeId' => new sfValidatorString(array('required' => false, 'max_length' => 10)), 'chkLogin' => new sfValidatorString(array('required' => false)), 'user_name' => new sfValidatorString(array('required' => false, 'max_length' => 20, 'trim' => true)), 'user_password' => new sfValidatorString(array('required' => false, 'max_length' => 20, 'trim' => true)), 're_password' => new sfValidatorString(array('required' => false, 'max_length' => 20, 'trim' => true)), 'status' => new sfValidatorString(array('required' => false))));
$this->getWidgetSchema()->setLabels($this->getFormLabels());
sfWidgetFormSchemaFormatterAddEmployee::setNoOfColumns(4);
//merge location dropdown
$formExtension = PluginFormMergeManager::instance();
$formExtension->mergeForms($this, 'addEmployee', 'AddEmployeeForm');
$this->widgetSchema->setFormFormatterName('AddEmployee');
}
示例8: date
/**
* Currently, PHP date() function always returns zeros for milliseconds (u)
* on Windows. This is a replacement function for date() which correctly
* displays milliseconds on all platforms.
*
* It is slower than PHP date() so it should only be used if necessary.
*/
private function date($format, $utimestamp)
{
$timestamp = floor($utimestamp);
$ms = floor(($utimestamp - $timestamp) * 1000);
$ms = str_pad($ms, 3, '0', STR_PAD_LEFT);
return date(preg_replace('`(?<!\\\\)u`', $ms, $format), $timestamp);
}
示例9: getSource
/**
* Gets the interesting lines in the interesting file
*/
public function getSource($nbLine = 6)
{
if (!is_file($this->file)) {
return;
}
$file = fopen($this->file, 'r');
$beginLine = max(0, $this->line - $nbLine / 2);
$endLine = $beginLine + $nbLine - 1;
$code = '';
$curLine = 0;
while ($line = fgets($file)) {
$curLine++;
if ($this->line == $curLine) {
$lineLabel = 'ERR:';
} else {
$lineLabel = str_pad($curLine, 3, '0', STR_PAD_LEFT) . ':';
}
if ($curLine >= $beginLine && $curLine <= $endLine) {
$code .= $lineLabel . $line;
}
if ($curLine > $endLine) {
break;
}
}
if (!preg_match('/^\\<\\?php/', ltrim($code))) {
$code = "<?php\n" . $code;
}
return $code;
}
示例10: mask
public static function mask($str, $padStr = '*', $show = 6, $pad = 10)
{
if (strlen($str) >= $show) {
$str = substr($str, 0, $show);
}
return self::out(str_pad($str, $pad, $padStr));
}
示例11: write
/**
* Write a data structure to an INI-formatted string or file.
* Adds "secure" comments to the start and end of the data so
* you can hide your INI data in files using a .php extension.
*/
public static function write($data, $file = false)
{
$out = "; <?php /*\n";
$write_value = function ($value) {
if (is_bool($value)) {
return $value ? 'On' : 'Off';
} elseif ($value === '0' || $value === '') {
return 'Off';
} elseif ($value === '1') {
return 'On';
} elseif (preg_match('/[^a-z0-9\\/\\.@<> _-]/i', $value)) {
return '"' . str_replace('"', '\\"', $value) . '"';
}
return $value;
};
$sections = is_array($data[current(array_keys($data))]) ? true : false;
if (!$sections) {
$out .= "\n";
}
foreach ($data as $key => $value) {
if (is_array($value)) {
$out .= "\n[{$key}]\n\n";
foreach ($value as $k => $v) {
$out .= str_pad($k, 24) . '= ' . $write_value($v) . "\n";
}
} else {
$out .= str_pad($key, 24) . '= ' . $write_value($value) . "\n";
}
}
$out .= "\n; */ ?>";
if ($file === false) {
return $out;
}
return file_put_contents($file, $out);
}
示例12: add_coupon_sub
function add_coupon_sub()
{
$_POST = $this->zaddslashes($_POST);
//如果是插入多条优惠码则执行以下
if (!empty($_POST['num'])) {
$number = date('mds') . str_pad(mt_rand(1, 999999), 7, '0', STR_PAD_LEFT);
//循环加入变量多条数据
for ($i = 0; $i < $_POST['num']; $i++) {
$data[$i]['name'] = $_POST['name'];
$data[$i]['coupon_code'] = 'Kshop' . mt_rand(0, 999) . $number . mt_rand(0, 999);
$data[$i]['discount'] = $_POST['discount'];
$data[$i]['validity_date'] = strtotime($_POST['validity_date']);
$data[$i]['status'] = $_POST['status'];
}
//循环插入多条数据
for ($i = 0; $i < count($data); $i++) {
$i == count($data) - 1 ? $success = 1 : ($success = 0);
$this->couponModel->getAddCouponDataStatus($data[$i]);
}
if ($success == 1) {
$this->success('添加优惠码成功!');
} else {
$this->error('添加优惠码失败!');
}
} else {
//如果是执行单条优惠码则执行以下
$_POST['validity_date'] = strtotime($_POST['validity_date']);
if ($this->couponModel->getAddCouponDataStatus($_POST)) {
$this->success('添加优惠码成功');
} else {
$this->error('添加优惠码失败');
}
}
}
示例13: Geohash
public function Geohash()
{
//build map from encoding char to 0 padded bitfield
for ($i = 0; $i < 32; $i++) {
$this->codingMap[substr($this->coding, $i, 1)] = str_pad(decbin($i), 5, "0", STR_PAD_LEFT);
}
}
示例14: parse_identifier_format
private function parse_identifier_format($identifier_format, $next_id, $left_pad)
{
if (preg_match_all('/{{{([^{|}]*)}}}/', $identifier_format, $template_vars)) {
foreach ($template_vars[1] as $var) {
switch ($var) {
case 'year':
$replace = date('Y');
break;
case 'month':
$replace = date('m');
break;
case 'day':
$replace = date('d');
break;
case 'id':
$replace = str_pad($next_id, $left_pad, '0', STR_PAD_LEFT);
break;
default:
$replace = '';
}
$identifier_format = str_replace('{{{' . $var . '}}}', $replace, $identifier_format);
}
}
return $identifier_format;
}
示例15: error
public static function error($message, $class = "N/A", $method = "N/A")
{
if (LOG_LEVEL == "ERROR") {
Utilities::checkSize(LOG_FOLDER . "debug.log");
error_log("/r/n[ERROR] " . date("Y-m-d h:i:s") . " - [" . str_pad($class, 20, " ") . "] - [" . str_pad($method, 20, " ") . "] - " . $message . "", 3, LOG_FOLDER . "error.log");
}
}