background preloader

PHP

Facebook Twitter

CakePHP: the rapid development php framework. Pages. Propel ORM. PHP Manual. String Functions. If you want a function to return all text in a string up to the Nth occurrence of a substring, try the below function.

String Functions

Works in PHP >= 5. (Pommef provided another sample function for this purpose below, but I believe it is incorrect.) <? Php function nsubstr($needle, $haystack, $n_occurrence) { $arr = explode($needle,$haystack,$n_occurrence); $last = count($arr) - 1; $pos_in_last = strpos($arr[$last],$needle); if ($pos_in_last ! $string = 'd24jkdslgjldk2424jgklsjg24jskgldjk24'; print 'S: ' . ? You will still have to make a conservative guess of the max number of characters per line with wordwrap(), but you can be more precise than if you were simply truncating a multiple-line string with substr(). See example: $text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. $wrapped_text = wordwrap($text,100,'<br>',true); $three_lines = nsubstr('<br>',$wrapped_text,3); print '<br><br>' .

$four_lines = nsubstr('<br>',$wrapped_text,4); print '<br><br>' . LDAP Functions. Date and Time functions. Most spreadsheet programs have a rather nice little built-in function called NETWORKDAYS to calculate the number of business days (i.e.

Date and Time functions

Monday-Friday, excluding holidays) between any two given dates. I couldn't find a simple way to do that in PHP, so I threw this together. It replicates the functionality of OpenOffice's NETWORKDAYS function - you give it a start date, an end date, and an array of any holidays you want skipped, and it'll tell you the number of business days (inclusive of the start and end days!) Between them. I've tested it pretty strenuously but date arithmetic is complicated and there's always the possibility I missed something, so please feel free to check my math.

The function could certainly be made much more powerful, to allow you to set different days to be ignored (e.g. <? Array Functions. Memcache Functions. Here is a simple memcached aggregator class which distributes the cache among multiple cache servers.

Memcache Functions

If a server fails, the load is redistributed automatically. It uses persistent connections. The constructor takes an array of arrays, with each inner array representing a server, with a 'server' (string) attribute that is the IP addres or host name of the memcached server, and a 'port' (int) attribute that is the port number on which memcached is running on the server. All of the existing memcached API functions are implemented except getStats() and getVersion(), which are server-specific. <? Symfony API. SimpleTest - Unit Testing for PHP.