:::: MENU ::::
Browsing posts in: Development

PHP: trim spaces in the middle of a string

When you let users input text in a textfield or input box it is always a good idea to use trim function to remove spaces form the beginning or the end of the string.

Today I needed a function to trim spaces from the middle of a string and replace with a single space.

    this will return the string where groups of consecutive spaces
    are collapsed in a single space

    eg. $str = "This is    my test    string";
    function will output "This is my test string"
function centerTrim($str){
    return preg_replace("/\s+/", " ", $str)

Continue Reading

MySQL: Select today, yesterday or day before yesterday records

Usually when you work with records where a date or time-stamp field is present you need to select records from:

  • today records
  • yesterday records
  • day before yesterday records

Lets say you have a table (my_table) like this:

ID Name Date
1 Jon Dow 2011-01-10
2 Jane Dow 2011-01-10

where `Date` is date type fieldWatch movie online John Wick: Chapter 2 (2017)

Today records:

FROM `my_table`

Continue Reading