<?php
if (isset($_POST['stuff']))
{
$stuff = explode(" ", $_POST['stuff']);
foreach ($stuff as $thing)
{
echo trim($thing) . "<br />";
}
echo "<br /><br />";
}
?>
<form action="" method="post">
<input type="text" name="stuff" style="width: 300px;" /><br />
<input type="submit" value="Submit" />
</form>
RegFee said:Thanks Dan. I put it here:
http://www.regfee.info/php/clean.php
NP won't let me rep you anymore, I must've liked something else you did
How would I go about modifying this code to use a large textbox, so I could, say, edit->select all and paste random stuff into it, and each word will be put on a new line?
<input type="text" name="stuff" style="width: 300px;" /><br />
<textarea cols="40" rows="10" name="stuff"></textarea><br />
<?php
if (isset($_POST['stuff']))
{
$stuff = explode(" ", $_POST['stuff']);
foreach ($stuff as $thing)
{
echo trim(ereg_replace("[^A-Za-z0-9]", "", $thing)) . "<br />";
}
echo "<br /><br />";
}
?>
<form action="" method="post">
<textarea cols="40" rows="10" name="stuff"></textarea><br />
<input type="submit" value="Submit" />
</form>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas vestibulum, nulla a rhoncus malesuada, nunc mauris sodales lectus, et ornare lectus eros sit amet massa.
Lorem
ipsum
dolor
sit
amet
consectetuer
adipiscing
elit
Maecenas
vestibulum
nulla
a
rhoncus
malesuada
nunc
mauris
sodales
lectus
et
ornare
lectus
eros
sit
amet
massa
OutDomain
ServicesNP
Domain
RegistrationNP
Domain
ManagementBuy
Domain
CreditDomain
ToolsWHOIS
Lookup
and
2007
Jelsoft
Enterprises
LtdSearch
Engine
Friendly
URLs
by
vBSEO
240
<?php
$ext = $_POST['ext'];
$minlength = $_POST['minlength'];
$maxlength = $_POST['maxlength'];
if ($maxlength < $minlength)
{
echo "The maximum length cannot be smaller than the minimum.<br />";
$_POST['stuff'] = null;
}
if (isset($_POST['stuff']))
{
$stuff = explode(" ", $_POST['stuff']);
foreach ($stuff as $thing)
{
$thing = ereg_replace("[^A-Za-z\n\s]", " ", $thing);
$thing2 = explode(" ", $thing);
foreach ($thing2 as $word)
{
$length = strlen(trim($word));
if ($length >= $minlength && $length <= $maxlength)
{
echo $word;
if ($ext != "none"){ echo "." . $ext;}
echo "<br />";
}
}
}
echo "<br /><br />";
}
else {echo "Enter text into the box below:";}
?>
<form action="" method="post">
<textarea cols="40" rows="10" name="stuff"></textarea><br />
Min Length:
<select name="minlength">
<option selected value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
</select>
Max Length:
<select name="maxlength">
<option selected value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
</select>
<br />
Choose an extension:
<select name="ext" size="-1">
<option selected value="none">None</option>
<option value="com">.com</option>
<option value="net">.net</option>
<option value="org">.org</option>
<option value="mobi">.mobi</option>
<option value="biz">.biz</option>
<option value="info">.info</option>
<option value="us">.us</option>
<option value="tv">.tv</option>
<option value="cc">.cc</option>
<option value="bz">.bz</option>
</select>
<br />
<input type="submit" value="Submit" />
</form>
<?php
// filter numeric input
$minlength = intval($_POST['minlength']);
$maxlength = intval($_POST['maxlength']);
// filter and format the extention if there is one
$ext = ($_POST['ext'] == 'none') ? '' : '.' . htmlentities($_POST['ext']);
// see if the user wants dashes and/or numbers too
$dashes = isset($_POST['dashes']) ? '-' : '';
$numbers = isset($_POST['numbers']) ? '0-9' : '';
if ($maxlength < $minlength)
{
echo "The maximum length cannot be smaller than the minimum.<br />";
$_POST['stuff'] = null;
}
if (isset($_POST['stuff']))
{
// construct our regular expression string to get only words with
// valid characters and of the min/max length
$preg = '/[A-Za-z' . $dashes . $numbers . ']{'. $minlength . ',' . $maxlength . '}/m';
// now match everything in the input
$stuff = array();
preg_match_all($preg,$_POST['stuff'],$stuff);
// process results only if we have them
if (count($stuff) != 0)
{
foreach ($stuff as $word)
echo $word, $ext, '<br />';
echo '<br /><br />';
}
}
else {echo 'Enter text into the box below:';}
?>
Dan said:I don't mind if you use it and I don't need any links.
The problem was with new lines. You could explode by new lines first, then loop through those and explode by spaces.
<?php
//Pull data from form input
$ext = $_POST['ext'];
$minlength = $_POST['minlength'];
$maxlength = $_POST['maxlength'];
//Check if invalid mins and maxes were used
if ($maxlength < $minlength)
{
echo "The maximum length cannot be smaller than the minimum.<br />";
$_POST['stuff'] = null;
}
//If form is submitted, run cleaning & generating code.
if (isset($_POST['stuff']))
{
//Create an array $stuff with each line of text
$stuff = explode("\n", $_POST['stuff']);
//Loop through array
foreach ($stuff as $thing)
{
//remove unneeded characters
$thing = ereg_replace("[^A-Za-z\n\s]", " ", $thing);
//Create a new array $thing with each word of text
$thing = explode(" ", $thing);
//Loop through $thing array
foreach ($thing as $word)
{
//get string length and only display if meets criteria
$length = strlen($word);
if ($length >= $minlength && $length <= $maxlength)
{
echo $word;
if ($ext != "none"){ echo "." . $ext;}
echo "<br />";
}
}
}
echo "<br /><br />";
}
?>
<form action="" method="post">
<textarea cols="40" rows="10" name="stuff"></textarea><br />
Min Length:
<select name="minlength">
<option selected value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
</select>
Max Length:
<select name="maxlength">
<option selected value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
</select>
<br />
Choose an extension:
<select name="ext" size="-1">
<option selected value="none">None</option>
<option value="com">.com</option>
<option value="net">.net</option>
<option value="org">.org</option>
<option value="mobi">.mobi</option>
<option value="biz">.biz</option>
<option value="info">.info</option>
<option value="us">.us</option>
<option value="tv">.tv</option>
<option value="cc">.cc</option>
<option value="bz">.bz</option>
</select>
<br />
<input type="submit" value="Submit" />
</form>
$thing = array_unique($thing);
...
$dashes = isset($_POST['dashes']) ? '-' : '';
$numbers = isset($_POST['numbers']) ? '0-9' : '';
...
$thing = ereg_replace("[^A-Za-z]", " ", $thing);
//why can't I just do this?
$thing = ereg_replace("[^A-Za-z".$numbers.$dashes."]", " ", $thing);
<?php
if (isset($_POST['stuff']))
{
$numbers = ($_POST['numbers']) ? '': '0-9';
$dashes = ($_POST['dashes']) ? '': "-";
$stuff = strtolower($_POST['stuff']);
$minlen = intval($_POST['minlength']);
$maxlen = intval($_POST['maxlength']);
$ext = $_POST['ext'];
$preg = '/\b[a-z'.$numbers.$dashes.']{'.$minlen.','.$maxlen.'}\b/si';
preg_match_all($preg, $stuff, $matches);
$array = array_unique($matches[0]);
foreach ($array as $word)
{
if ($word != "")
{
if ($ext == "none") echo "$word<br />\n";
else echo "$word.$ext<br />\n";
}
}
}
?>
<form action="" method="post">
<textarea cols="40" rows="10" name="stuff"></textarea><br />
Min Length:
<select name="minlength">
<option selected value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
</select>
Max Length:
<select name="maxlength">
<option selected value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
</select>
<br />
Choose an extension:
<select name="ext" size="-1">
<option selected value="none">None</option>
<option value="com">.com</option>
<option value="net">.net</option>
<option value="org">.org</option>
<option value="mobi">.mobi</option>
<option value="biz">.biz</option>
<option value="info">.info</option>
<option value="us">.us</option>
<option value="tv">.tv</option>
<option value="cc">.cc</option>
<option value="bz">.bz</option>
</select>
<br />
Exclude:
<input type="checkbox" name="numbers" /> Numbers <input type="checkbox" name="dashes" /> Dashes</br />
<br />
<input type="submit" value="Submit" />
</form>
RegFee said:Cef, I didn't even see your response! I tried it out, but it is just showing "array". I was reading up on the trim function, and it said if you try to trim an array, it will just display the word array, is this a similar problem?
foreach ($stuff as $word)
foreach ($stuff[0] as $word)
cef said:Not sure which of all the above you went with, but for posterity, here's the fix for the typo in the code I posted.
Change this:
PHP:foreach ($stuff as $word)
To this:
PHP:foreach ($stuff[0] as $word)
<?php
if (isset($_POST['stuff']))
{
$numbers = ($_POST['numbers']) ? '': '0-9';
$dashes = ($_POST['dashes']) ? '': "-";
$stuff = strtolower($_POST['stuff']);
$minlen = intval($_POST['minlength']);
$maxlen = intval($_POST['maxlength']);
if ($minlen > $maxlen) $maxlen = $minlen;
$ext = ($_POST['ext'] !== 'none') ? '.' . $_POST['ext'] : '';
$preg = '/\b[a-z'.$numbers.$dashes.']{'.$minlen.','.$maxlen.'}\b/si';
preg_match_all($preg, $stuff, $matches);
$array = array_unique($matches[0]);
foreach ($array as $word)
echo $word, $ext, '<br />';
}
else
{
// set defaults for the form controls
$numbers = '0-9';
$dashes = '-';
$stuff = '';
$minlen = 3;
$maxlen = 3;
$ext = '';
}
// available domain extentions
$extentions = array(
'none',
'com',
'net',
'org',
'mobi',
'biz',
'info',
'us',
'tv',
'cc',
'bz'
);
// build a select list of sizes with a selected entry
function build_size_list($min,$max,$selected)
{
for ($i=$min; $i <= $max; $i++)
echo '<option ', $i == $selected ? ' selected="selected" ' : '', 'value="', $i, '">', $i , "</option>\n";
}
// build the extension list with a selected entry
function build_extention_list($extention_array,$selected)
{
foreach($extention_array as $extention)
echo '<option ',
($extention === 'none' && $selected === '') || ($selected === ('.' . $extention)) ?
' selected="selected" ' : '', 'value="', $extention, '">', $extention , "</option>\n";
}
?>
<form action="" method="post">
<textarea cols="40" rows="10" name="stuff"><?php echo htmlentities($stuff); ?></textarea><br />
Min Length:
<select name="minlength">
<?php build_size_list(3,18,$minlen); ?>
</select>
Max Length:
<select name="maxlength">
<?php build_size_list(3,18,$maxlen); ?>
</select>
<br />
Choose an extension:
<select name="ext" size="-1">
<?php build_extention_list($extentions,$ext); ?>
</select>
<br />
Exclude:
<input type="checkbox" name="numbers" <?php echo $numbers == '' ? ' checked="checked" ' : ''; ?> /> Numbers
<input type="checkbox" name="dashes" <?php echo $dashes == '' ? ' checked="checked" ' : ''; ?> /> Dashes</br />
<br />
<input type="submit" value="Submit" />
</form>
