Changing one digit number to three digit number...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peregrino
Forum Newbie
Posts: 6
Joined: Thu Nov 26, 2009 6:02 am

Changing one digit number to three digit number...

Post by peregrino »

hi Everyone,
I am very new to PHP programming. I only started learning it now because I have been put in charge of the website of the volunteer organization that I work for. I am mostly using Joomla to set up the website.

Anyway, here is the challenge I have. I have a php file that I want to modify. I'll paste the code at the bottom of this post. It is a plugin that automatically creates links whenever someone has written a scripture reference e.g. Matthew 6:24 .

The problem I have is that for the chapter code, it uses the actual number displayed in the link (e.g. "6" for the example I gave above). What i want it to do is to generate a THREE DIGIT number (e.g. "006" in the example above).

I think the way to do it is with an "array" of some sort that will change 6 to 006, but I don't know how to do it. I have tried playing around with the code and copying the other arrays that are there, but I am not having any luck.

The code that seems to generate the chapter number is:

Code: Select all

.rtrim($alink[0],":").
, but as I said, I don't have the faintest idea of how to change the numbers to three digit ones. I can manually write an array (if that is what is needed) for each number, as I only have to go up to 150 (since no book in the Bible has more than 150 chapters), but I would need to know how to do it.

I am such a newbie! Is there anyone who can help me out with this? Thanks.

Code: Select all

defined( '_JEXEC' ) or die( 'Restricted access' );
 
$mainframe->registerEvent( 'onPrepareContent', 'scripture_text' );
 
function scripture_text( &$row, &$params, $page=0 ) 
{
    $db =& JFactory::getDBO();
    $plugin =& JPluginHelper::getPlugin('content', 'scripturelinks');
    $st_params  = new JParameter( $plugin->params );
    if ( !$st_params->get( 'enabled', 1 ) ) {
        return true;
    }
  $bible_version = $st_params->get( 'bible_version' );
  $replace_all = $st_params->get( 'replace_all' );
  $row->text = html_entity_decode($row->text);
  if($replace_all){
  $books = getBooksRegex($bible_version);
  $row->text = preg_replace('#{/?bible\s?\d?\d?}#i','',$row->text);
  $st = "/\b(?=\S)(".$books.")\s(\d{1,3})[:]?(\d{1,3})?\s?[-]?\s?(\d{1,3})?[:]?(\d{1,3})?\b/i";
  $row->text = preg_replace_callback( $st,'link_insert',$row->text );
  return true;
  } else {
  $st =  "#{bible\s*(.*?)}(.*?){/bible}#";
  $row->text = preg_replace_callback( $st,'link_insert',$row->text );
  return true;
  }
 }
 
function link_insert(&$matches)
{ 
  $plugin =& JPluginHelper::getPlugin('content', 'scripturelinks');
  $li_params    = new JParameter( $plugin->params );
  $bible_version = $li_params->get( 'bible_version' );
  $replace_all = $li_params->get( 'replace_all' );
  $audio_link = $li_params->get( 'audio_link' );
  $audio_source_aid = $li_params->get( 'audio_source_aid' );
  $audio_source_aid = explode(",",$audio_source_aid);
  $source = $audio_source_aid[0];
  $aid = $audio_source_aid[1];
  $use_gb = $li_params->get( 'use_gb' );
  $scripture = $matches[0];
  $relarray = array('','');
  if($use_gb) $relarray = sl_gb_popup();
  if(!$replace_all){
    $bible_version = $matches[1];
    $scripture = $matches[2];
    }
  $books = getBooksRegex($bible_version);
  $st = "/\b(".$books.")\s(\d{1,3})[:]?(\d{1,3})?\s?[-]?\s?(\d{1,3})?[:]?(\d{1,3})?\b/i";
  $alink = preg_replace_callback( $st,'split_verse',$scripture );
  $alink = explode(",",$alink); 
  $windowopen = "window.open(this.href,this.target,'width=550,height=350,scrollbars=1');return false;";
  $scripture_link = "";
  $scripture_link = '<a href="http://jesus-teachings.com/bible/'.rtrim($alink[2]).'_'.rtrim($alink[0],":").'.html" target="_blank" onclick="'.$windowopen.'"'.$relarray[0].'>{STRING}</a>';
  for($i=1; $i<count($matches); $i++){
    $scripture_link = str_replace('{STRING}', html_entity_decode(trim($scripture)), $scripture_link);
    $scripture_link = str_replace('{QUERY}', urlencode(html_entity_decode($scripture)), $scripture_link);
  $awindowopen = "window.open(this.href,this.target,'width=100,height=100');return false;";
  
if($audio_link){
    if($aid == '4' || $aid == '21'){
    $scripture_link .= ' <a title="Audio" href="http://www.biblegateway.com/resources/audio/flash_play.php?source='.$source.'&aid='.$aid.'&book='.rtrim($alink[2]).'&chapter='.rtrim($alink[0],":").'&end_chapter='.rtrim($alink[1],":").'" target="_blank" onclick="'.$awindowopen.'"'.$relarray[1].'><img style="WIDTH: 16px; HEIGHT: 9px" height="9" src="http://www.biblegateway.com/resources/audio/images/sound.gif" width="16" border="0" /></a> ';
    }
    else {
    $scripture_link .= ' <a title="Audio" href="http://www.biblegateway.com/resources/audio/real_play.php?source='.$source.'&aid='.$aid.'&book='.rtrim($alink[2]).'&chapter='.rtrim($alink[0],":").'&end_chapter='.rtrim($alink[1],":").'" target="_blank" onclick="'.$awindowopen.'"'.$relarray[1].'><img style="WIDTH: 16px; HEIGHT: 9px" height="9" src="http://www.biblegateway.com/resources/audio/images/sound.gif" width="16" border="0" /></a> ';
    }
  } else {$scripture_link .= ' ';}
    return $scripture_link;
  }
}
 
function split_verse(&$match)
{
  $plugin =& JPluginHelper::getPlugin('content', 'scripturelinks');
  $sv_params    = new JParameter( $plugin->params );
  $bible_version = $sv_params->get( 'bible_version' );
  $book = strtolower($match[1]);
  $biblebooks = getBooks($bible_version);
  $key = sl_array_search_recursive($book, $biblebooks, true);
  $cstart = $match[2];
  if(isset($match[4])){
    $cstop = $match[4];
  }else{
    $cstop = $match[2];
  }
  $linkstr = "";
  $linkstr = $cstart.','.$cstop.','.$key[0];
  return $linkstr;
}
 
function sl_array_search_recursive( $needle, $haystack, $type=false )
{
   $path = NULL;
   $keys = array_keys($haystack);
   while (!$path && (list($toss,$k)=each($keys))) {
      $v = $haystack[$k];
      if (is_scalar($v)) {
         if (strtolower($v)===strtolower($needle)) {
            $path = array($k);
         }
      } elseif (is_array($v)) {
         if ($path = sl_array_search_recursive( $needle, $v )) {
            array_unshift($path,$k);
         }
      }
   }
   return $path;
}
 
function getBooksRegex($bible_version){
  $langfile = getLang($bible_version);
  if (file_exists(dirname(__FILE__).DS.'scripturelinks'.DS.$langfile.'.php')) {
      include_once(dirname(__FILE__).DS.'scripturelinks'.DS.$langfile.'.php');
  } else {
      include_once(dirname(__FILE__).DS.'scripturelinks'.DS.'EN.php');
  }
        $books = ''._book1.'|Gen|'._book2.'|Exod\.?|'._book3.'|Lev\.?|'.
                ''._book4.'|Num\.?|'._book5.'|Deut\.?|'.
                ''._book6.'|Josh?\.?|'._book7.'|Judg\.?|'._book8.'|'._book9.'|'._book10.'|1?2? Sam\.?|'.
                ''._book11.'|'._book12.'|1?2? Kgs\.?|'._book13.'|'._book14.'|1?2? Chro\.?|'._book15.'|'.
                ''._book16.'|Neh\.?|'._book17.'|Tob\.?|'._book18.'|Jdt.?|'._book19.'|Esth\.?|'._book20.'|1Ma\.?|'.
                ''._book21.'|2Ma\.?|'._book22.'|'._book23.'|Psa\.?|'._book24.'|Prov\.?|'._book25.'|Ecc\.?|'.
                ''._book26.'|Song\.?|'._book27.'|Wis\.?|'._book28.'|Sir\.?|'._book29.'|Isa\.?|'._book30.'|Jer\.?|'.
                ''._book31.'|Lam\.?|'._book32.'|Bar\.?|'._book33.'|Ezek\.?|'._book34.'|'.
                'Dan\.?|'._book35.'|'._book36.'|'._book37.'|'._book38.'|Obad\.?|'._book39.'|'.
                ''._book40.'|'._book41.'|'._book42.'|Hab\.?|'._book43.'|'.
                'Zeph\.?|'._book44.'|Hag\.?|'._book45.'|Zech\.?|'._book46.'|Mal\.?|'.
                ''._book47.'|Matt\.?|'._book48.'|'._book49.'|'._book50.'|Jn\.?|'.
                ''._book51.'|'._book52.'|Rom\.?|'._book53.'|'._book54.'|1?2? Cor\.?|'.
                ''._book55.'|Gal\.?|'._book56.'|Eph\.?|'.
                ''._book57.'|Phil\.?|'._book58.'|Col\.?|'._book59.'|'._book60.'|'.
                '1?2? Thes\.?|'._book61.'|'._book62.'|1?2? Tim\.?|'._book63.'|Tit\.?|'._book64.'|Phile\.?|'.
                ''._book65.'|Heb\.?|'._book66.'|Jas\.?|'._book67.'|'._book68.'|1?2? Pet\.?|'.
                ''._book69.'|'._book70.'|'._book71.'|1?2?3? Jn\.?|'._book72.'|'.
                ''._book73.'|Rev\.?|';
  return $books;
}
 
function getBooks($bible_version)
{
  $langfile = getLang($bible_version);
  if (file_exists(dirname(__FILE__).DS.'scripturelinks'.DS.$langfile.'.php')) {
      include_once(dirname(__FILE__).DS.'scripturelinks'.DS.$langfile.'.php');
  } else {
      include_once(dirname(__FILE__).DS.'scripturelinks'.DS.'EN.php');
  }
            $biblebooks = array (
                'genesis' => array ('abrev' => 'gen', 'book' => ''._book1.''),         
                'exodus' => array ('abrev' => 'exod', 'book' => ''._book2.''),                
                'leviticus' => array ('abrev' => 'lev', 'book' => ''._book3.''),
                'numbers' => array ('abrev' => 'num', 'book' => ''._book4.''),         
                'deutoronomy' => array ('abrev' => 'deut', 'book' => ''._book5.''),         
                'joshua' => array ('abrev' => 'josh', 'book' => ''._book6.''),
                'judges' => array ('abrev' => 'judg', 'book' => ''._book7.''),         
                'ruth' => array ('abrev' => 'ruth', 'book' => ''._book8.''),               
                '1_samuel' => array ('abrev' => '1 sam', 'book' => ''._book9.''),               
                '2_samuel' => array ('abrev' => '2 sam', 'book' => ''._book10.''),               
                '1_kings' => array ('abrev' => '1 kgs', 'book' => ''._book11.''),
                '2_kings' => array ('abrev' => '2 kgs', 'book' => ''._book12.''),
                '1_chronicles' => array ('abrev' => '1 chro', 'book' => ''._book13.''),      
                '2_chronicles' => array ('abrev' => '2 chro', 'book' => ''._book14.''),      
                'ezra' => array ('abrev' => 'ezra', 'book' => ''._book15.''),      
                'nehemiah' => array ('abrev' => 'neh', 'book' => ''._book16.''),             
                'tobit' => array ('abrev' => 'tobit', 'book' => ''._book17.''),
                'judith' => array ('abrev' => 'judith', 'book' => ''._book18.''),
                'esther' => array ('abrev' => 'esther', 'book' => ''._book19.''),      
                20 => array ('abrev' => '1 maccabees', 'book' => ''._book20.''),      
                21 => array ('abrev' => '2 maccabees', 'book' => ''._book21.''),      
                'job' => array ('abrev' => 'job', 'book' => ''._book22.''),      
                'psalms' => array ('abrev' => 'psa', 'book' => ''._book23.''),           
                'proverbs' => array ('abrev' => 'prov', 'book' => ''._book24.''),            
                'ecclesiastes' => array ('abrev' => 'ecc', 'book' => ''._book25.''),
                'song_of_solomon' => array ('abrev' => 'song', 'book' => ''._book26.''),  
                'wisdom' => array ('abrev' => 'wisdom', 'book' => ''._book27.''),               
                'sirach' => array ('abrev' => 'sirach', 'book' => ''._book28.''),               
                'isaiah' => array ('abrev' => 'isa', 'book' => ''._book29.''),               
                'jeremiah' => array ('abrev' => 'jer', 'book' => ''._book30.''),
                'lamentations' => array ('abrev' => 'lam', 'book' => ''._book31.''),    
                'baruch' => array ('abrev' => 'baruch', 'book' => ''._book32.''),             
                'ezekiel' => array ('abrev' => 'ezek', 'book' => ''._book33.''),             
                'daniel' => array ('abrev' => 'dan', 'book' => ''._book34.''),
                'hosea' => array ('abrev' => 'hosea', 'book' => ''._book35.''),      
                'joel' => array ('abrev' => 'joel', 'book' => ''._book36.''),      
                'amos' => array ('abrev' => 'amos', 'book' => ''._book37.''),      
                'obadiah' => array ('abrev' => 'obad', 'book' => ''._book38.''),        
                'jonah' => array ('abrev' => 'jonah', 'book' => ''._book39.''),      
                'micah' => array ('abrev' => 'micah', 'book' => ''._book40.''),      
                'nahum' => array ('abrev' => 'nahum', 'book' => ''._book41.''),      
                'habakkuk' => array ('abrev' => 'hab', 'book' => ''._book42.''),             
                'zephaniah' => array ('abrev' => 'zeph', 'book' => ''._book43.''),
                'haggai' => array ('abrev' => 'hag', 'book' => ''._book44.''),          
                'zechariah' => array ('abrev' => 'zech', 'book' => ''._book45.''),           
                'malachi' => array ('abrev' => 'mal', 'book' => ''._book46.''),
                'matthew' => array ('abrev' => 'matt', 'book' => ''._book47.''),        
                'mark' => array ('abrev' => 'mark', 'book' => ''._book48.''),      
                'luke' => array ('abrev' => 'luke', 'book' => ''._book49.''),      
                'john' => array ('abrev' => 'john', 'book' => ''._book50.''),      
                'acts' => array ('abrev' => 'acts', 'book' => ''._book51.''),      
                'romans' => array ('abrev' => 'rom', 'book' => ''._book52.''),               
                '1_corinthians' => array ('abrev' => '1 cor', 'book' => ''._book53.''),
                '2_corinthians' => array ('abrev' => '2 cor', 'book' => ''._book54.''),
                'galatians' => array ('abrev' => 'gal', 'book' => ''._book55.''),       
                'ephesians' => array ('abrev' => 'eph', 'book' => ''._book56.''),            
                'philippians' => array ('abrev' => 'phil', 'book' => ''._book57.''),
                'colossians' => array ('abrev' => 'col', 'book' => ''._book58.''),      
                '1_thessalonians' => array ('abrev' => '1 thes', 'book' => ''._book59.''),       
                '2_thessalonians' => array ('abrev' => '2 thes', 'book' => ''._book60.''),       
                '1_timothy' => array ('abrev' => '1 tim', 'book' => ''._book61.''),
                '2_timothy' => array ('abrev' => '2 tim', 'book' => ''._book62.''),
                'titus' => array ('abrev' => 'tit', 'book' => ''._book63.''),
                'philemon' => array ('abrev' => 'phile', 'book' => ''._book64.''),     
                'hebrews' => array ('abrev' => 'heb', 'book' => ''._book65.''),              
                'james' => array ('abrev' => 'jas', 'book' => ''._book66.''),
                '1_peter' => array ('abrev' => '1 pet', 'book' => ''._book67.''),
                '2_peter' => array ('abrev' => '2 pet', 'book' => ''._book68.''),
                '1_john' => array ('abrev' => '1 jn', 'book' => ''._book69.''),
                '2_john' => array ('abrev' => '2 jn', 'book' => ''._book70.''),
                '3_john' => array ('abrev' => '3 jn', 'book' => ''._book71.''),
                'jude' => array ('abrev' => 'jude', 'book' => ''._book72.''),
                'revelation' => array ('abrev' => 'rev', 'book' => ''._book73.'')
            );
  return $biblebooks;
}
 
function getLang($bv)
{
      $langarray = array (
          1 => array('booknum' => '94','lang' => 'AMU'),
          2 => array('booknum' => '28','lang' => 'AR'),
          3 => array('booknum' => '82','lang' => 'BG'),
          4 => array('booknum' => '21','lang' => 'BG'),
          5 => array('booknum' => '90','lang' => 'CCO'),
          6 => array('booknum' => '23','lang' => 'CPF'),
          7 => array('booknum' => '29','lang' => 'CS'),
          8 => array('booknum' => '11','lang' => 'DA'),
          9 => array('booknum' => '54','lang' => 'DE'),
          10 => array('booknum' => '33','lang' => 'DE'),
          11 => array('booknum' => '10','lang' => 'DE'),
          12 => array('booknum' => '48','lang' => 'EN'),
          13 => array('booknum' => '8','lang' => 'EN'),
          14 => array('booknum' => '45','lang' => 'EN'),
          15 => array('booknum' => '46','lang' => 'EN'),
          16 => array('booknum' => '16','lang' => 'EN'),
          17 => array('booknum' => '63','lang' => 'EN'),
          18 => array('booknum' => '47','lang' => 'EN'),
          19 => array('booknum' => '77','lang' => 'EN'),
          20 => array('booknum' => '9','lang' => 'EN'),
          21 => array('booknum' => '49','lang' => 'EN'),
          22 => array('booknum' => '78','lang' => 'EN'),
          23 => array('booknum' => '76','lang' => 'EN'),
          24 => array('booknum' => '31','lang' => 'EN'),
          25 => array('booknum' => '64','lang' => 'EN'),
          26 => array('booknum' => '50','lang' => 'EN'),
          27 => array('booknum' => '74','lang' => 'EN'),
          28 => array('booknum' => '51','lang' => 'EN'),
          29 => array('booknum' => '65','lang' => 'EN'),
          30 => array('booknum' => '72','lang' => 'EN'),
          31 => array('booknum' => '53','lang' => 'EN'),
          32 => array('booknum' => '15','lang' => 'EN'),
          33 => array('booknum' => '57','lang' => 'ES'),
          34 => array('booknum' => '41','lang' => 'ES'),
          35 => array('booknum' => '58','lang' => 'ES'),
          36 => array('booknum' => '59','lang' => 'ES'),
          37 => array('booknum' => '42','lang' => 'ES'),
          38 => array('booknum' => '60','lang' => 'ES'),
          39 => array('booknum' => '61','lang' => 'ES'),
          40 => array('booknum' => '6','lang' => 'ES'),
          41 => array('booknum' => '32','lang' => 'FR'),
          42 => array('booknum' => '2','lang' => 'FR'),
          43 => array('booknum' => '69','lang' => 'GRC'),
          44 => array('booknum' => '68','lang' => 'GRC'),
          45 => array('booknum' => '70','lang' => 'GRC'),
          46 => array('booknum' => '81','lang' => 'HE'),
          47 => array('booknum' => '71','lang' => 'HIL'),
          48 => array('booknum' => '62','lang' => 'HR'),
          49 => array('booknum' => '17','lang' => 'HU'),
          50 => array('booknum' => '18','lang' => 'IS'),
          51 => array('booknum' => '3','lang' => 'IT'),
          52 => array('booknum' => '55','lang' => 'IT'),
          53 => array('booknum' => '34','lang' => 'IT'),
          54 => array('booknum' => '20','lang' => 'KO'),
          55 => array('booknum' => '4','lang' => 'LA'),
          56 => array('booknum' => '24','lang' => 'EN'),
          57 => array('booknum' => '56','lang' => 'NDS'),
          58 => array('booknum' => '109','lang' => 'NGU'),
          59 => array('booknum' => '30','lang' => 'NL'),
          60 => array('booknum' => '5','lang' => 'NO'),
          61 => array('booknum' => '35','lang' => 'NO'),
          62 => array('booknum' => '12','lang' => 'PL'),
          63 => array('booknum' => '25','lang' => 'PT'),
          64 => array('booknum' => '37','lang' => 'PT'),
          65 => array('booknum' => '14','lang' => 'RO'),
          66 => array('booknum' => '13','lang' => 'RU'),
          67 => array('booknum' => '39','lang' => 'RU'),
          68 => array('booknum' => '40','lang' => 'SK'),
          69 => array('booknum' => '1','lang' => 'SQ'),
          70 => array('booknum' => '44','lang' => 'SV'),
          71 => array('booknum' => '7','lang' => 'SV'),
          72 => array('booknum' => '75','lang' => 'SW'),
          73 => array('booknum' => '43','lang' => 'TL'),
          74 => array('booknum' => '27','lang' => 'UK'),
          75 => array('booknum' => '19','lang' => 'VI'),
          76 => array('booknum' => '80','lang' => 'ZHS'),
          77 => array('booknum' => '22','lang' => 'ZHT')
      );
  $keyarr = sl_array_search_recursive($bv, $langarray);
  $key = $keyarr[0];
  $langfile = $langarray[$key]['lang'];
  return $langfile;
}
 
function sl_gb_popup() 
{
  ?><script type="text/javascript">
      var GB_ROOT_DIR = "<?php echo JURI::base();?>plugins/content/scripturelinks/greybox/";
  </script>
  <script type="text/javascript" src="plugins/content/scripturelinks/greybox/AJS.js"></script>
  <script type="text/javascript" src="plugins/content/scripturelinks/greybox/AJS_fx.js"></script>
  <script type="text/javascript" src="plugins/content/scripturelinks/greybox/gb_scripts.js"></script>
  <link href="plugins/content/scripturelinks/greybox/gb_styles.css" rel="stylesheet" type="text/css" />
  <?php
  $relarray = array(' rel="gb_page[800, 450]"',' rel="gb_page_center[500, 300]"');
  return $relarray;
}
 
?>
 
User avatar
el_gato
Forum Newbie
Posts: 12
Joined: Sun Oct 18, 2009 9:28 pm
Location: Bandung
Contact:

Re: Changing one digit number to three digit number...

Post by el_gato »

Hai peregrino, to change one to any number of digit, you can use the following function:

Code: Select all

 
function zeroExtend($s, $n)
{
    for ($i = 0; $i < $n; $i++) {
        if (strlen($s) == $n) return $s;
        $s = "0".$s;
    }
 
    return $s;
}
 
//ex:
$num = zeroExtend(6, 3); //will result $num = 006
 
hope it helps you
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Changing one digit number to three digit number...

Post by Mark Baker »

$chapter = str_pad($chapter,3,'0',STR_PAD_LEFT);
peregrino
Forum Newbie
Posts: 6
Joined: Thu Nov 26, 2009 6:02 am

Re: Changing one digit number to three digit number...

Post by peregrino »

thanks for the quick replies. I'll try out the code either today or tomorrow, though I am slightly confused as to where to put the code and whether both codes offered compliment each other, or whether they are both different solutions to the same problem.

I really don't know, but I suspect that Gato's code is for changing ALL one digit numbers to three digits. If that is the case, then the problem with that is that I don't want to change the verse numbers, but only the chapter numbers.

Mark Baker's code seems to be specifically for the chapter number, but I am still not sure how to put it in place.

In any case, I'll have a go at moving code around and seeing what works and report on it later. I do appreciate at least a pointer in the right direction as I really have no clue about this.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Changing one digit number to three digit number...

Post by requinix »

Why don't people give sprintf any love?

Code: Select all

sprintf("%03u", $number)
peregrino
Forum Newbie
Posts: 6
Joined: Thu Nov 26, 2009 6:02 am

Re: Changing one digit number to three digit number...

Post by peregrino »

Why don't people give sprintf any love?
I am quite happy to give it some love if you can kindly tell me how to use it to fix the problem i have. :P
peregrino
Forum Newbie
Posts: 6
Joined: Thu Nov 26, 2009 6:02 am

Re: Changing one digit number to three digit number...

Post by peregrino »

Mark Baker wrote:$chapter = str_pad($chapter,3,'0',STR_PAD_LEFT);
I tried putting this quote in, but I got the following message:
Notice: Undefined variable: chapter in C:\wamp\www\my_site\plugins\content\scripturelinks.php on line 43
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Changing one digit number to three digit number...

Post by requinix »

peregrino wrote:
Why don't people give sprintf any love?
I am quite happy to give it some love if you can kindly tell me how to use it to fix the problem i have. :P
Give it a $number < 1000 and it'll give you the same number padded to three digits.

If that rtrim($alink[0],":") is what needs to change (seems that way) then replace it with

Code: Select all

sprintf("%03u", rtrim($alink[0],":"))
Or using the other examples,

Code: Select all

zeroExtend(rtrim($alink[0],":"), 3)
str_pad(rtrim($alink[0],":"),3,'0',STR_PAD_LEFT)
peregrino
Forum Newbie
Posts: 6
Joined: Thu Nov 26, 2009 6:02 am

Re: Changing one digit number to three digit number...

Post by peregrino »

el_gato wrote:Hai peregrino, to change one to any number of digit, you can use the following function:

Code: Select all

 
function zeroExtend($s, $n)
{
    for ($i = 0; $i < $n; $i++) {
        if (strlen($s) == $n) return $s;
        $s = "0".$s;
    }
 
    return $s;
}
 
//ex:
$num = zeroExtend(6, 3); //will result $num = 006
 
hope it helps you
Hi El Gato... I tried putting in your code and nothing happened. I didn't get any error message, but the url generated by the scripturelink plugin was still one digit.

Anyone else have any ideas?
peregrino
Forum Newbie
Posts: 6
Joined: Thu Nov 26, 2009 6:02 am

Re: Changing one digit number to three digit number...

Post by peregrino »

tasairis wrote:
peregrino wrote:
Why don't people give sprintf any love?
I am quite happy to give it some love if you can kindly tell me how to use it to fix the problem i have. :P
Give it a $number < 1000 and it'll give you the same number padded to three digits.

If that rtrim($alink[0],":") is what needs to change (seems that way) then replace it with

Code: Select all

sprintf("%03u", rtrim($alink[0],":"))
Or using the other examples,

Code: Select all

zeroExtend(rtrim($alink[0],":"), 3)
str_pad(rtrim($alink[0],":"),3,'0',STR_PAD_LEFT)
Tasairis, you are a GENIUS!! THANK YOU!! That first line worked perfectly! 8) Thanks for your help!!
Post Reply