
Sometime it is useful to be able to extract the first word of a sentence contained in one of the columns of your table. Luckily for us, MySQL makes this a trivial operation thanks to its useful SUBSTRING_INDEX() function. From the reference manual: SUBSTRING_INDEX(str,delim,count) – Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim. So for our purposes, if we want to select the first word from a sentence or string of words, the natural delimiter we would be looking out for is ‘ ‘ – i.e. an empty space character. Because we’re interested in the first word of the sentence, we want everything returned before the first occurrence of the space, leaving us with sql which looks like this: Nifty.

To generate a string containing a random selection of both letters and numbers (i.e. an alphanumeric string) using PHP is pretty trivial. Essentially what we want to do is define a string containing all the characters we wish to use in the generated string. Then randomly select characters from the string and glue them all together until we get a random string of the desired length. Coded as a function, you get: Pretty simple, but quite useful actually.

Considering I work with PHP day in and day out, it is no wonder that I completely forgot that JavaScript also comes up with its super handy substring function, for when you need to grab only a specific part of a string (much like PHP’s substr function). string.substring(from, to) The JavaScript substring() method extracts the characters from a string, between two specified indices, and returns the new sub string. In extracts the characters in a string between “from” and “to”, not including “to” itself. Two parameters, the first required, the second optional. In our example above, from indicates the index where to start the extraction – remember, the first character is at index 0. The second, optional parameter is to, which if specified indicates the index where to stop the extraction. If omitted, substring extracts the rest of the string. In action: var str=”Hello world!”; document.write(str.substring(3)+”"); document.write(str.substring(3,7)); The above will output: lo world! lo w Useful.

To get the last character of a given string in PHP is made very, very simple thanks to the plain old vanilla substring (substr) function. When you call substr, you feed it the string, the starting point and the number of characters you want it to return. If you leave out the number of characters to return, then the function returns all the characters from the starting point until the end of the string. Knowing this, we can then figure out that if we want the last character of a string, we simply make the starting point the length of the string minus 1 to take into account the zero-based character position counter. In practice: echo substr(‘My String’,strlen(‘My String’) – 1); //returns g However, substr makes this EVEN easier by allowing us to specify a NEGATIVE starting point. This indicates that the function should count towards the left starting from the last character instead of the usual counting to the right starting from the first character. Armed with this knowledge, our call to retrieve the last character in the string now becomes: echo substr(‘My String’,-1); //returns g Feeding a -3 would return the last three characters of the string, meaning our above example would print out ‘ing’. And now you know! :)

Sometimes it comes in quite handy to strip out all the non-alphanumeric characters from a given string. Of course, we could just use a bog standard preg_replace with a valid regex to achieve this, though the result of doing this might result in a non-readable resulting string.

Taking Andrew Walker’s previously mentioned handy little UTF-16 to UTF-8 string converter function, we now have in our means a particularly easy way in which to craft a simple UTF-16 to UTF-8 file converter.

Andrew Walker crafted this handy little PHP function which can convert a UTF-16 encoded string into a more PHP-friendly UTF-8 encoded string.

Born in her native Czech Republic back in 1980, it took Denise Milani 21 years to move out to the United States of America, but once she did, boy did her stardom take off like a rocket. This glamour, bikini and fashion model is known for her impossibly large natural breasts and very fit body, making her a very, very popular (and searched for) girl! For this particular swimsuit shoot, Denise shows off her tight body in this great little green and black string bikini!

Minami Matsumaka is a Japanese gravure idol. Born on August in 1992, Minami has appeared in bikini and lingerie magazines, calendars, sexy photoshoots and print ads. She had also released a gravure video and photobooks. For this particular shoot, Minami appears wearing a great little pink string bikini.

Minami Matsumaka is a Japanese gravure idol. Born on August in 1992, Minami has appeared in bikini and lingerie magazines, calendars, sexy photoshoots and print ads. She had also released a gravure video and photobooks. For this particular shoot, Minami appears wearing a great little white string bikini.

While most modern programming languages do feature a function that allows you to insert one string into another string at any given position, PHP for some or other reason simply doesn’t. So while you might be well versed in using a good old insert(str,x) function in any of the other languages that you might be used to, doing the same in PHP will no doubt leave you scratching your head as you frantically browse through all of the PHP string functions in their nifty online manual while trying to find the elusive little bugger. So if no native string insert function exists, how exactly does one go about doing it then? Well, the solution lies in the creative use of the PHP substring replace function (substr_replace). Essentially our goal is to insert our insert string into the original string by replacing a ‘substring’ of length zero at the desired position in the original string. The syntax for doing this would then be: So for example if we wanted to inject ‘my’ into the classic sentence ‘Hello world!’, you would code: Which would then result in ‘Hello my world!’ appearing on the screen. (Note, if you enter a negative position then the string is inserted so many characters from the end of the original string.) Nifty

Sexy Japanese gravure model Yoko Kumada certainly knows how to turn up the heat, stepping out in this stunning white string bikini.
Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under whichever martial arts dojo is closest at the time, and for the most part, simply enjoys life with his amazing wife and daughter.
Oh, and he draws ever now and then too.
This is a collection of things that he has managed to find the time to scribble down since 2007.
Looking for Something?
Jump to Category: