string

Create a URL slug from any string

Okay, so automically generating a friendly URL for a blog post or forum post from their titles doesn't really take a lot - but this snippet might make it easier for you:

function make_slug($orig, $length = 0)
{
  $slug = preg_replace('/[^a-zA-Z0-9]/', '-', $orig);
  $slug = preg_replace('/\-\-+/', '-', $slug);
  $slug = strtolower($slug);
 
  if ($length > 0) {
    // limits the length of the slug
    $slug = substr($slug, 0, $length);
  }
 
  if (strrpos($slug, '-') == strlen($slug) - 1) {
    // removes any ending -
    $slug = substr($slug, -1);
  }

Snippet: function intToRoman()

A fun little function that will produce the Roman Numeral representation for any number from 1-3999.

The reason for the limitation is because the Romans used barred letters for the number 5000 and larger, and the representation of 4000 would be MV, which is hard to represent in a simple ASCII string (maybe as an exercise to the reader, the function can be extended?).

function intToRoman($number)
{
    if (!is_numeric($number)) {
        echo "Input is not a number\r\n\r\n";
        return;
    }
 

Snippet: function astrstr()

Following up on the astrpos() function, here is an astrstr() function:

Documentation - astrpos()

Description

  • int astrpos ( string $haystack, array $needles [, $offset [, int $flags ] ] )

    Returns the numeric position of either the first needle found in the haystack, or the first instance of a needle in the haystack.

Parameters

  • haystack

    The string to search in

  • needle

    An array of strings to search in the haystack for

  • offset

Examples - astrpos()

<?php
 
include_once('astrpos.php'); // or however you want to include it
 
$needles = array(
 'apple',
  'banana',
 'pear',
 'orange'
);
 
$haystack = 'The fruiterer has some bananas and apples today. Unfortunately, there are no oranges, but the pears will be in tomorrow.';
 
// this will return the position of 'banana' - the first needle in the haystack
echo astrpos($haystack, $needles) . '<br>';
 
// this will return the position of 'apple' - the first needle in the array of needles
echo astrpos($haystack, $needles, 0, ASTR_NEEDLE_ORDER) . '<br>';
 

Source Code - astrstr()

<?php
 
/**
 * function astrstr()
 *
 * Function to find the first instance of any needle from an array of strings 
 * within a string
 * 
 *
 * @author      Stuart Jones <stuart@random-tweak.co.uk>
 * @copyright   Copyright 2008 Stuart Jones
 * @version    08.11.25
 * @license   <a href="http://www.gnu.org/licenses/gpl.html<br />
" title="http://www.gnu.org/licenses/gpl.html<br />
">http://www.gnu.org/licenses/gpl.html<br />
</a> * 
 * @param    string          $haystack
 * @param    array&lt;string&gt;   $needles
 * @param   [int          $offset]
 * @param   [int          $flags]
 */
 
 
// define constants for function flags 
if (!defined('ASTR_NEEDLE_ORDER')) { define('ASTR_NEEDLE_ORDER', 1); }

Documentation - astrstr()

Description

  • int astrstr ( string $haystack, array $needles [, $offset [, int $flags ] ] )
    Returns the first instance of either the first needle found in the haystack, or the first instance of a needle in the haystack.

Parameters

  • haystack

    The string to search in

  • needle

    An array of strings to search in the haystack for

  • offset

Examples - astrstr()

<?php
 
include "astrstr.php";
 
$needles = array(
  'apple',
  'banana',
 'pear',
 'orange',
);
 
$haystack = 'The fruiterer has some bananas and apples today. Unfortunately, there are no oranges, but the pears will be in tomorrow.';
 
// this will return the string from the first instance of 'banana' 
// the first needle in the haystack
echo astrstr($haystack, $needles) . "\r\n";
 
// this will return the string upto the first instance of 'banana' 
// the first needle in the haystack
echo astrstr($haystack, $needles, 0, ASTR_BEFORE_NEEDLE) . "\r\n";
 

Snippet: function astrpos()

I found myself trying to locate a PHP function in their library last week that didn’t seem to exist. I had a number of strings in an array, and needed to simply find out if one of them was in a string – and I needed to do it at various points. However, I could only find functions that let me search for a single needle in an array of haystacks.

So I made one myself:

Source Code - astrpos()

Source code: Function astrpos()

<?php
 
/**
 * function astrpos()
 *
 * Function to find the position of the first instance of any needle 
 * from an array of strings within a string
 * 
 *
 * @author      Stuart Jones <stuart@random-tweak.co.uk>
 * @copyright   Copyright 2008 Stuart Jones
 * @version   08.10.13
 * @license   <a href="http://www.gnu.org/licenses/gpl.html<br />
" title="http://www.gnu.org/licenses/gpl.html<br />
">http://www.gnu.org/licenses/gpl.html<br />
</a> * 
 * @param    string          $haystack
 * @param    array&lt;string&gt;   $needles
 * @param   [int          $offset]
 * @param   [int          $flags]
 */
 
 
// define constants for function flags