Monthly Archives: Jul 2018

Ensure only a UK decimal value is added to a textbox – JavaScript

If you are adding a price, or a value to a textbox input, you may need to sanitise it.  This script will make sure that nothing is allowed in the box except for a decimal point and numeric values.  It will also ensure that there is only two numbers after the decimal place and that there can only be one decmial place.

$('#someTextInput').keypress(function(event) {
        var $this = $(this);
        if ((event.which !== 46 || $this.val().indexOf('.') !== -1) &&
            ((event.which < 48 || event.which > 57) &&
                (event.which !== 0 && event.which !== 8))) {
            event.preventDefault();
        }

        var text = $(this).val();
        if ((event.which === 46) && (text.indexOf('.') === -1)) {
            setTimeout(function() {
                if ($this.val().substring($this.val().indexOf('.')).length > 3) {
                    $this.val($this.val().substring(0, $this.val().indexOf('.') + 3));
                }
            }, 1);
        }

        if ((text.indexOf('.') !== -1) &&
            (text.substring(text.indexOf('.')).length > 2) &&
            (event.which !== 0 && event.which !== 8) &&
            ($(this)[0].selectionStart >= text.length - 2)) {
            event.preventDefault();
        }
    });

    $('#someTextInput').bind("paste", function(e) {
        var text = e.originalEvent.clipboardData.getData('Text');
        if ($.isNumeric(text)) {
            if ((text.substring(text.indexOf('.')).length > 3) && (text.indexOf('.') > -1)) {
                e.preventDefault();
                $(this).val(text.substring(0, text.indexOf('.') + 3));
            }
        }
        else {
            e.preventDefault();
        }
    });

 

Share this post:
read more

Queso Cheese Dip (Mexican Cheese Dip)

The ultimate “smother over nachos” dip.

This cheese dip is amazing.  It reminds me of the dip that comes smothered over nachos in cinemas and other places.  It has that delicious, moreish taste that you want to keep eating.

It does pay you to slowly cook the garlic and onions. I mean, slowly. I think I spent over 20 minutes over a shallow heat softening everything up.

One final note is that I like a spicy dip and with the amount of Jalopeno’s below, it is quite tangy.  You can increase or decrease this amount to taste.

Ingredients

  • 225 g Cheddar Cheese
  • 1 tbsp cornflour
  • 8 g butter
  • One large garlic clove, minced
  • 1/4 small white onion, very finely chopped
  • 375g evaporated milk
  • One small tomato, very finely diced
  • 1/4 tsp onion powder
  • 1/4 tsp garlic powder
  • 1/4 tsp cumin
  • 113g jar Jalapeno Peppers
  • 1/4 cup coriander/cilantro, finely chopped
  • 2 – 3 tbsp milk
  • Salt

Instructions

  1. Toss the cheese and cornflour in a bowl to coat.
  2. In a heavy bottom saucepan, melt the butter, then add the garlic/onion, cooking until both are soft, but neither are brown.
  3. Add tomato, juices and all, and cook for 2 minutes until tomato softens.
  4. Add evaporated milk and cheese. Stir, then add chiles and Spices.
  5. Add the cheese, stirring continuously.
  6. Add salt to taste – the amount required depends on the saltiness of cheese used
  7. Stir through the coriander.
  8. Add a splash of milk to thin down the sauce.  The sauce will thicken as it cools, so some trial and error may be required.
  9. Remove from heat. Serve warm or at room temperature – it will be soft and scoopable even when it cools.
Share this post:
read more

Explode a multi-dimensional array (PHP)

php-banner

Sometimes you need to explode more than just a single dimension array.  In the case of a project I worked on recently, I was given this data, taken from MySQL in the form of a string.

bromsgrove:27.83|crawley:153.34|manchester:119.5|peterborough:125.62|

So, this is four locations, with a respective distance.

The trick with splitting them up, using PHP explode(), is to do it in two parts.  In this case, I first want to split them into units of warehouse location and distance, which is achieved like this:

$firstArray= explode('|',$distanceData->distance);

This will then produce an array that looks like this:

array (size=5)
  0 => string 'bromsgrove:27.83' (length=16)
  1 => string 'crawley:153.34' (length=14)
  2 => string 'manchester:119.5' (length=16)
  3 => string 'peterborough:125.62' (length=19)
  4 => string '' (length=0)

The problem with this, of course, is that the delimiter is added to the end of every location that is concatenated onto the end of the string when the data is being produced, so the next step is to get rid of the last character, which [in this case and many others], will always be tagged onto the end of a string like this.  We can do this with PHP substr().

$distanceData = substr($distanceData,0,-1);

So now when the array is produced, it doesn’t have the empty slot at the end.

array (size=4)
  0 => string 'bromsgrove:27.83' (length=16)
  1 => string 'crawley:153.34' (length=14)
  2 => string 'manchester:119.5' (length=16)
  3 => string 'peterborough:125.62' (length=19)

We now have 4 arrays that all need exploding, so it’s simply a case of cycling through each with a foreach loop and creating a new exploded array each time:

foreach($firstArray as $key => $data)
{
   $distance[$key]= explode(':',$data);
}

Which will give the final result we are looking for, a fully split-up, two-dimensional array, created from a delimited string.

array (size=4)
  0 => 
    array (size=2)
      0 => string 'bromsgrove' (length=10)
      1 => string '27.83' (length=5)
  1 => 
    array (size=2)
      0 => string 'crawley' (length=7)
      1 => string '153.34' (length=6)
  2 => 
    array (size=2)
      0 => string 'manchester' (length=10)
      1 => string '119.5' (length=5)
  3 => 
    array (size=2)
      0 => string 'peterborough' (length=12)
      1 => string '125.62' (length=6)

Although this array is OK, it does still need some work to make it super easy to use.  This can be done by whittling it down further, whilst already inside the foreach loop.

First, initialise an array:

$hubDistance= array();

Then build this array as you loop through using the data provided from the explode.

foreach ($firstArray as $key => $data) {
    $distance[$key] = explode(':', $data);
    $hubDistance[$distance[$key][0]] = (float)$distance[$key][1];
}

This will strip out any of the unnecessary levels in the array and leave you with something like this:

array (size=4)
  'bromsgrove' => float 27.83
  'crawley' => float 153.34
  'manchester' => float 119.5
  'peterborough' => float 125.62

Which is much better!

I’ve explicitly called float on the numbers for this project, but PHP will typically sort this out for you.

Update:

I keep needing to use this on various projects, so I’ve whittled it down to a function if anybody needs to use it:

function stringToArray($string)
{

    /*
     * This relies on the string being in the format
     * a1:a1a¬b1:b1a¬c1:c1a¬ [etc]
     * Individual separator = :
     * Group / cluster separator = ¬
     */

    $string = mb_substr($string,0 ,-1);

    $firstArray = explode("¬",$string);

    $cleanArray = array();

    foreach ($firstArray as $key => $data) {
        $innerArray[$key] = explode(':', $data);
        $cleanArray[$innerArray[$key][0]] = $innerArray[$key][1];
    }

    return $cleanArray;

} 

So if you input a string like this :

$inputString = "sw4:33¬sw5:44¬swnh4:23¬swnh5:0¬mw4:223¬mw5:233¬mwnh4:433¬mwnh5:45¬"

Then use the function:

var_dump(stringToArray($inputString);

You’ll end up with something that looks like this:

array (size=8)
  'sw4' => string '33' (length=2)
  'sw5' => string '44' (length=2)
  'swnh4' => string '23' (length=2)
  'swnh5' => string '0' (length=1)
  'mw4' => string '223' (length=3)
  'mw5' => string '233' (length=3)
  'mwnh4' => string '433' (length=3)
  'mwnh5' => string '45' (length=2)

Probably no use to anybody, but it’s there if you need it 🙂
 

Share this post:
read more