Monthly Archives: Feb 2018

How to capitalise the first word of each sentence – PHP

php-banner

So this can be a tricky one.  PHP has some excellent built-in functions for capitalising the first word of a string and capitalising all of the words in a string, but there are no built-in functions (that I’m aware of) that will capitalise the first word of each sentence.  This can be overcome using our old favourites, the regular expressions and preg_replace_callback() function.

$outputString = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {
    return strtoupper($matches[1] . ' ' . $matches[2]);
}, ucfirst(strtolower($inputString)));

 

Share this post:
read more

Stop jQuery ignoring the if/else conditions

You may or may not have come across this.  If you are using an AJAX call and have added an if/else statement to the success: parameter, and in particular have bound some actions to an element within those clauses, it can carry on and run the else, even if the if has been satisfied.

Example:

success: function(result) {

            if(result == "success")
            {
                window.location.replace('/somewhere_else');
            }
            else {
                $("#errorMsg").append(result);
            }

        }

This can be avoided if you terminate the jQuery with a false return.

Example:

success: function(result) {

            if(result == "success")
            {
                window.location.replace('/portal');
                return false;
            }
            else {
                $("#errorMsg").append(result);
            }

        }

It will halt the jQuery in its tracks, before it can do something odd.

This is observational behaviour and could well be something that I am doing that is causing this in the first place, so if it is, and somebody would be kind enough to point it out, I would be very obliged.

Share this post:
read more

Centre a form using Bootstrap 4 native controls

That pesky login form that simply refuses to be in the middle.  Not a problem, see the example below:

<div class="row h-75 justify-content-center align-items-center">
        <form>
            <div class="form-group">
                <label for="formGroupExampleInput">Example label</label>
                <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input">
            </div>
            <div class="form-group">
                <label for="formGroupExampleInput2">Another label</label>
                <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input">
            </div>
        </form>
    </div>

The h-XXX after the row attribute is the height of the box.  A small number, a higher box.  If this number is set to 100, the form will be centred.

Share this post:
read more