• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

PHP word to words

Hi i need some script where i can put word like website backup now and it give me few result like back up website now, now website backup, now backup website.
0
umaxim
Asked:
umaxim
  • 2
  • 2
2 Solutions
 
Julian HansenCommented:
You can try this.

$string = 'backup website now';
$words = explode(' ', $string);

dump('', $words);

function dump($prefix, $words)
{
  if (count($words) == 0) {
    echo $prefix . "\n";
    return;
  }
  for($i = 0; $i< count($words); $i++){
    $output = $prefix . $words[0] . ' ';
    $copy = $words;
    array_shift($copy);
    dump($output, $copy);
    array_push($words, array_shift($words));
  }
}

Open in new window

0
 
Ray PaseurCommented:
It is easy to rearrange words:

website backup now => backup website now => website now backup, etc...

This can be done with the standard combination algorithm.  See:
http://www.laprbass.com/RAY_temp_umaxim.php

It is not so easy to handle language-specific stemming and affixes; for that you need a grammatical awareness and dictionary.  Example... How do you know where to break these words?

website backup now => back up web site now

Here's the combinations algorithm.

<?php // RAY_combinations.php
error_reporting(E_ALL);
echo "<pre>";

// GENERATE THE COMBINATIONS OF CHARACTERS IN A STRING.
// ADAPTED FROM http://cogo.wordpress.com/2008/01/08/string-permutation-in-php/
function string_combinations($str)
{
    if (strlen($str) < 2) return array($str);

    $combos = array();
    $endpop = substr($str, 1);

    // RECURSE
    foreach (string_combinations($endpop) as $permutation)
    {
        $length = strlen($permutation);
        for ($i = 0; $i <= $length; $i++)
        {
            $combos[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
        }
    }
    return $combos;
}

// GENERATE THE COMBINATIONS OF ELEMENTS IN A ONE-DIMENSIONAL ARRAY
function array_combinations($arr)
{
    $return = array();

    // GET THE NUMERIC KEY RANGE OF THE INPUT ARRAY
    $str = implode('', range(0, count($arr)-1));

    // GET THE KEY COMBINATIONS
    $combos = string_combinations($str);

    // ITERATE OVER THE COMBINATIONS
    foreach ($combos as $combo)
    {
        $out = array();
        while (strlen($combo))
        {
           $key   = substr($combo,0,1);
           $combo = substr($combo,1);
           $out[] = $arr[$key];
        }
        $return[] = $out;
    }
    return $return;
}

// ARRAYS: DEMONSTRATE THE WORK PRODUCT
$abc = array('backup', 'website', 'now');
$arr = array_combinations($abc);
print_r($arr);

Open in new window

HTH, ~Ray
0
 
Julian HansenCommented:
@Ray looks like a bit of a long winded way of achieving the same result as the code in my post does.
0
 
Ray PaseurCommented:
@julianH: Yes, it has more code and comments. It is more generalized in that it can handle strings without specific delimiters as well as arrays.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now