[PHP] Help mee php versies op 1 lijn te brengen

Pagina: 1
Acties:

  • red_indian
  • Registratie: Maart 2000
  • Laatst online: 24-06 16:43

red_indian

 Smokin' Suckz B!

Topicstarter
ik merkte dat bepaalde functies die thuis wel beschikbaar zijn, niet beschikbaar zijn op mijn betaalde php host (mevershosting). volgens mij draaien ze daar een andere versie en heb ik daarom bepaalde functies niet.

nu had ik het volgende idee. als iedereen die wel eens handmatig een 'standaard functie' heeft uitgecoded hem hier nu post, dan hebben we een centraal punt waar dit soort functies snel terug te vinden zijn in het geval dat we er weer eens één missen. *D

hier de eerste twee 'standaard functies' die ik mis bij mijn host:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?
if (!function_exists("bcmod"))
    {
function bcmod($operand, $modulus) {
  /* bcmod --  Get modulus of an arbitrary precision number
     string bcmod (string left operand, string modulus)
     Get the modulus of the left operand using modulus. */

  $div = intval($operand / $modulus);
  $res = $operand - ($div * $modulus);
  return $res;
}
    }

if (!function_exists("array_search"))
    {
function array_search ($item, $array) {
  /* array_search --  Searches the array for a given value and 
     returns the corresponding key if successful. */

  for ($pos = 0; $pos < count ($array); $pos++)  if ($item == $array[$pos]) return $pos;
  return false;
}
    }
?>

opmerking:
ik heb '!function_exists' toegevoegd zodat je ze allemaal in één include bestand kunt gooien en op de host wel uitgezocht wordt welke nu wel en niet geinclude moet worden. zijn hier nog performance issues te merken of valt dit wel mee? ik vind het persoonlijk wel makkelijk dat ik ze dan thuis en op mijn host kan draaien zonder eerst weer die include aan te passen.

let op! de array_search is niet *exact* hetzelfde, die van mij ken nog geen 'strict' optie en returned een integer ipv de key. op zich is hij makkelijk aan te passen. misschien doe ik dat nog wel ff. voordeel van deze oplossing is wel dat hij nu op de meest simpele host nog werkt.

de standaard ziet er zo uit:
mixed array_search (mixed needle, array haystack [, bool strict])

Need a cuesheet/tracklisting? (meer dan 500.000 online)


  • red_indian
  • Registratie: Maart 2000
  • Laatst online: 24-06 16:43

red_indian

 Smokin' Suckz B!

Topicstarter
heb hem aangepast:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?
if (!function_exists("array_search2"))
    {
function array_search2 ($needle, $haystack, $strict = false, $first = false) {
  /* array_search --  Searches the array for a given value and 
     returns the corresponding key if successful. */

  if (!is_array($haystack)) return;
  $res = array_keys ($haystack, $needle);
  if ($first) $res = $res[0]; else $res = $res[count($res) - 1];
  
  if (!empty($res) || is_int($res)) {
    if ($strict) {
      if ($needle === $haystack[$res]) return $res;
    } else return $res;
  }
}
    }
?>

het blijkt dat de standaard array_search het laatste element oplevert, bij deze kun je kiezen, vandaar de extra optie $first.

[update]
als ik een verzoekje mag doen? array_chunk kent mijn php versie ook niet standaard. dus als iemand die heeft liggen / wil maken, plz do! :+
[/update]

Need a cuesheet/tracklisting? (meer dan 500.000 online)