PDA

View Full Version : سوال: ساخت تابع



kol michelson
چهارشنبه 14 تیر 1391, 13:06 عصر
سلام!
نمیدونم اسمش اسکریپته یا تابع، ولی میخوام یه چیزی درست بشه که بهش یک یا چند کلمه داده بشه بعد با در نظر گرفته شدن حروف سازنده ی اون کلمات، اونا رو جابه جا کنه و کلمات جدید بسازه.
مثلن وقتی کلمه ی " صدا " رو وارد می کنی، کلمات : اصد - صاد - ادص - دصا - داص رو بهمون بده !
اگه کسی میتونه چنین چیزی بسازه بهم ایمیل بده یا با ایدی من در تماس باشه : taking98@yahoo.com

مرسی:قلب:

blackmak
چهارشنبه 14 تیر 1391, 13:35 عصر
<pre>
<?php
function permute($str)
{
/* If we only have a single character, return it */
if (strlen($str) < 2)
{
return array($str);
}
/* Initialize the return value */
$permutations = array();
/* Copy the string except for the first character */
$tail = substr($str, 1);
/* Loop through the permutations of the substring created above */
foreach (permute($tail) as $permutation)
{
/* Get the length of the current permutation */
$length = strlen($permutation);
/* Loop through the permutation and insert the first character of the original
string between the two parts and store it in the result array */
for ($i = 0; $i <= $length; $i++)
{
$permutations[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
}
}
/* Return the result */
return $permutations;
}
$str = 'siamak';
print_r(permute($str));
?>

منبع : How can I get all unique combinations of a word's characters? (http://stackoverflow.com/questions/6797578/how-can-i-get-all-unique-combinations-of-a-words-characters)
موفق باشید