Jump to content

Cut-up engine


sano

Recommended Posts

You mean a program that creates anagrams.
Do they have to be meaningful (a word) or just a random mix up?

If you are looking only for a random mix up, you can just use the function [url=http://www.php.net/manual/en/function.str-shuffle.php]str_shuffle()[/url].
But if you want to make in words out of the inputed word, it's a bit more complicated but I got an idea how to do.

Orio.
Link to comment
https://forums.phpfreaks.com/topic/24002-cut-up-engine/#findComment-109047
Share on other sites

First, you recieve the variable from a form (let's say the method you used is post, and the field's name was "words"):
$string=$_POST['words'];

Then you split it into an array using [url=http://www.php.net/manual/en/function.explode.php]explode()[/url] (by spaces):
$words=explode(" ", $string);

Then you use [url=http://www.php.net/manual/en/function.shuffle.php]shuffle()[/url] to randomize the order:
shuffle($words);

Then you use [url=http://www.php.net/manual/en/function.implode.php]implode()[/url] to join back the elements, using the glue as space:
$result=implode(" ",$words);

Finally, print the result:
echo $result;



Whole code looks like this:
[code]<?php

$string=$_POST['words'];

$words=explode(" ", $string);
shuffle($words);
$result=implode(" ",$words);

echo $result;

?>[/code]


Hope it helps :)


Orio.
Link to comment
https://forums.phpfreaks.com/topic/24002-cut-up-engine/#findComment-109108
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.