Updated Simple PHP Scraping Function

Going back over some of my old posts on web scraping and looking through the code I’ve noticed a few places where there is room for improvement. One such example is the scrape_between() function used in Working With The Scraped Data.

Functionally, it works. But it’s not great.

  • Repeatedly overwriting the $data variable isn’t a great idea.
  • It’s working on the assumption that both the $start and $end string are found in the search string, i.e. if $start isn’t found then the function should be terminating and returning false, not continuing on performing operations on an empty variable. This isn’t necessarily that bad, but it’s not very elegant and could become a performance issue if the function is used in a large appliction processing thousands of pages.

With that said, here’s my rewrite, which is more readable, makes more sense and is structurally more sound.

 * String Extract PHP Function
 * Simple function for extracting a string from within a string, given a start and end point.
 * Copyright (c) 2014 Jacob Ward (http://www.jacobward.co.uk)
 * Licensed under the MIT (http://opensource.org/licenses/MIT) and GPL (http://www.gnu.org/copyleft/gpl.html) licenses.

    function stringExtract($item, $start, $end) {
    	if (($startPos = stripos($item, $start)) === false) {	// If $start string is not found
    		return false;	// Return false
    	} else if (($endPos = stripos($item, $end)) === false) {	// If $end string is not found
    		return false;	// Return false
    	} else {
    		$substrStart = $startPos + strlen($start);	// Assigning start position
    		return substr($item, $substrStart, $endPos - $substrStart);	// Returning string between start and end positions

I’ve changed the name of the function and added some info before the function as it’s now hosted on GitHub as php-string-extract.

Just thought I’d post it here too for the sake of doing so.

By the way, I haven’t forgotten about the epic post I promised, I’m working on it most days but don’t have a great amount of free time at the moment. But trust me, the wait will be worth it!

Get Category ID From Category Name Function [WordPress]

Working on a new WordPress plugin for a client, I found the need to get a category’s ID from the category’s name.

Surprisingly, I found that there was no WordPress function to accomplish this.

So, here’s the little one I came up with to solve the problem:

	// Function to retrieve category ID, given name - http://www.jacobward.co.uk
	function get_category_id($category_name) {
		$term = get_term_by('name', $category_name, 'category');
		return $term->term_id;

Add this into your functions.php file or, if developing a plugin, into your plugin file.

Call it by passing your category name, like:

	// Assign category ID of 'Category Name' to $category_id variable
	$category_id = get_category_id('Category Name');

Age Validation Function [PHP]

The most recent college project I’ve had is to develop a system for a video games shop. The last task was to evaluate the system then recommend and implement any improvements.

One idea I had was for validating whether a customer stored in the database is in fact old enough to be purchasing the games in their order, so I wrote a little PHP function to do just that.

	// Function to calculate whether customer is old enough to purchase product – DOB input in format YYYY-MM-DD
	// Created by Jacob Ward (http://www.jacobward.co.uk) - Please leave this in if you choose to use this in your projects.

	function validate_age($dob, $restriction) {
		$dates = explode("-", $dob);	// Exploding sections of date into array
		$year = date("Y") - $dates["0"];	// Subtracting entered year from current year
		$month = date("m") - $dates["1"];	// Subtracting entered month from current month
		$day = date("d") - $dates["2"];	// Subtracting entered day from current day
		// If month is negative, means it's a year earlier - Decrement year by 1. Else if month is 0 and day is negative, means it's a year earlier - Decrement year by 1
		if ($month < 0) {
		} elseif ($month == 0 && $day < 0) {
		// If customer's age is greater than or equal to certificate then age is valid, else it's invalid
		if ($year >= $restriction) {	
			$valid_age = TRUE;
		} else {
			$valid_age = FALSE;
		return $valid_age;	// Return TRUE or FALSE whether customer is old enough to purchase product

It should be pretty simple to understand, but I’ll talk it through.

The function takes two, a date of birth (in the format of YYYY-MM-DD) and the age restriction, e.g.

$dob = "1985-11-18";
$age_restriction = 18;

validate_age($dob, $age_restriction);

It then returns TRUE if the validation is successful or FALSE if it is unsuccessful.

And that’s it! Hope it comes in handy at some point.