Will Fris's WordPress Weblog











{20150810}   array_sprintf

Do you know of a quicker way to do this?
I’d be glad to know so šŸ™‚

The following code does a sprintf on a collection.


	if ( !function_exists( 'array_sprintf' ) ) {
		function array_sprintf ( $templateString , $inputArray ) {
			reset( $inputArray );
			while ( $i = each( $inputArray ) ) {
				if ( !is_array( $i[ 'value' ] ) ) {
					$i[ 'value' ] = array( $i[ 'value' ] );
				}
				array_unshift( $templateString , $i[ 'value' ] );
				$inputArray[ $i[ 'key' ] ] = call_user_func_array(
					'sprintf'
					, $i[ 'value' ]
				);
			}
			return $inputArray;
		}
	}

Obligatory example:


	$tags = array(
		array( 'href' => 'https://willfris.wordpress.com/' , 'txt' => 'This blog' )
		, [ 'http://willfris.nl/' , 'A site' ] # shorthand array-syntax
	);
	$element = [ '<li>' , '</li>' ];
	echo '<ul>' . $element[ 0 ]
		. implode(
			implode( $element )
			, array_sprintf( '<a href="%s">%s</a>' , $tags )
		)
		. $element[ 1 ] . '</ul>';


{20150724}   php’s nl2br is evil

Line-breaks <br /> are something for poetry and stuff.
Use paragraphs <p />.
They make up a <section /> or <article />.


if ( !function_exists( 'nl2p' ) ) {
    function nl2p ( $text ) {
        $peed = ''
            . '<p>'
            . str_replace(
                  array( "\r\n" , "\r" , "\n" )
                , '</p><p>'
                , $text
            )
            . '</p>'
        ;
        return $peed;
    }
}


{20110910}   getters, setters

Ć©Ć©n functienaam voor beide zaken

functionaliteit wordt bepaald aan de hand van invoer

mogelijke wijzen van invoer:

  1. geen invoer, ergo: getter

    mogelijke teruggave’s:

    • altijd een array in geval van samengestelde gegevens
    • altijd een single value van een specifiek datatype bij enkel gegeven
    • eventueel NULL
  2. invoer, ergo: setter

    • enkele invoer( Ć©Ć©n gegeven parameter)

      • setter is voor samengestelde gegevens
        1. enkele invoer is array

          verschillende invoeren worden nieuwe waarden van gegevens tenzij waarde van invoer gelijk is aan NULL

          over het algemeen zal/zullen hierbij de oude waarde/-n behouden blijven

        2. enkele invoer is geen array en geen NULL

          enkele invoer wordt de nieuwe waarde van de betreffende eigenschap

          over het algemeen zal/zullen hierbij de oude waarde/-n behouden blijven

      • setter is voor enkel gegeven

        invoer wordt nieuwe waarde voor gegeven

    • meerdere invoer( meer dan Ć©Ć©n meegegeven parameter)

      • setter is voor samengestelde gegevens

        verschillende invoeren worden de nieuwe waarden van gegevens tenzij de waarde van de invoer gelijk is aan NULL

        over het algemeen zal/zullen hierbij de oude waarde/-n behouden blijven

      • setter is voor een enkel gegeven

        mĆ g niet voorkomen, dus een foutmelding/exception.



While working in PHP today, I wanted to have a way to turn a string like:
name=value,name=,name=value
(maybe even: name=value,name,name=value,=value,name=value )
I tried:
explode('=' , explode(',' , $array));
But unfortunately that did not work, since explode() does not except an array as argument. (And the small fact that I got the parameters mixed up first time around did not help either. šŸ™‚ )

I could not immediately come up with a way to do this, so I searched for something like: `php recursive explode`.
Among others, I found `PHP split or explode`.
One of the questions that poster asks is: “Is it recommended to avoid to use the split() function and other PHP functions ?”
To answer this, I went to the php split-function page in the manual, which I can do by typing: `p split` in the location bar since I use firefox and made `p` the keyword for this search engine.
(I actually type: `:o p split`, since I am using the vimperator add-on for firefox, maybe more about that some other time though.)

Nonetheless, that page did not answer my question either.
I however digressed as I do very often, going off to write this blog-entry and to answer my new question:
What is the difference between str_split and explode ?
Well , in short to this is:

str_split
splits a string by a certain amount of chars and returns an array

while

explode
splits a string by a certain other string and returns an array

.
This however did not solve the problem , so I searched a bit more: `explode to associative array` .
Which gave me some useful hits that were good for hints, but did not provide a way to do key-value_pairs.

I thought it took too long to find a solution, so I wrote my own, in the neglect of it being an `unelegant` solution.
So for now, this is my solution to the problem:


<?php
#I have absolutely no idea why explode() should have it's delimiter as first parameter(as it says in the manual).
function splita ($string , $delimiters = ' ')
	{
		$array                                      =  array();
		is_array($delimiters) ? NULL : $delimiters  =  array_slice(func_get_args() , 1);//Check if array is empty?
		if (count($delimiters) !== 2){throw new Exception('Sorry, not yet supported.');}
		reset($delimiters);
		$arr                                        =  explode(current($delimiters) , $string);
		next($delimiters);
		foreach ($arr as $index => $name_value_pair)
			{
				$tmp  =  explode(current($delimiters) , $name_value_pair);
				if ($tmp === FALSE)
					{//echo 'geen value:' . implode(':',$tmp);
						$array[$tmp[0]]  =  '';
					}
				elseif (isset($tmp[1]))
					{//echo 'name-value:' . implode(':',$tmp);
						$array[$tmp[0]]  =  $tmp[1];
					}
				else
					{//echo 'just-value:' . implode(':',$tmp);
						$array[]         =  $tmp[0];
					}
			}
		return $array;
	}
?>

I will probably change it again sometime, there’s also stuff I did not take into account yet.
Anyway, for now, it will do.

Update (Sun 2009-08-23, 15:55.47 CEST):
I knew it…
The parse_str() function… doh!



To make some comparison between CodeIgniter, CI from here on, and Symfony Framework, SF from here on, I decided to go through some of the first hits of Google for a change.

In general I can read in most posts that:
Both of the frameworks have superb documentation, which is very nice, thumbs up!

CI is nice and lightweight, while SF is nice and heavyweight
which just makes CI more responsive then SF
SF is all about best practises whereas CI seems to take a more pragmatic route
SF is in php5, CI can be in php4 _and_ php5
SF is mainly meant for big ass applications because of the way it is set up with a cli to create applications, modules, and what not more, while CI is more meant to be a great library with an easy to understand setup

I also read that CI has gotten a fork called Kohana a all php5-strict framework.
I will most certainly use this framework in a future project to get the feel of it.

I’ve started using Symfony now;
I wanted to have a InnoDB table (dunno why. :P) and I wanted it to be in utf8 charset and utf8_bin collation (dunno why. :S)
I found my InnoDB info here:
http://www.symfony-framework.com/2007/10/18/mysql-connection-configuration-schema-and-model-generation/
And the utf8 stuff can be found in chapter 8 of the symfony manual/book/or whatever.



et cetera