NameSilo

Looking for a program that will...

Spaceship Spaceship
Watch

RegFee

No FateVIP Member
Impact
223
I'm looking for a program that will take a bunch of randomly spaced text and place all of the words into separate lines.

for example:

asdf asdfasdf asdfasdfasdf.

Will be cleaned into:
asdf
asdfasdf
asdfasdfasdf

Thanks!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
<?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>
 
0
•••
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?
 
0
•••
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?

Just replace this line in Dan's code:

Code:
 <input type="text" name="stuff" style="width: 300px;" /><br />

with this:

Code:
<textarea cols="40" rows="10" name="stuff"></textarea><br />
 
1
•••
Thanks for moving it to the programming forum, mods. I didn't place it there at first because I was thinking an actual program might exist that does this, but PHP works just fine.

Thanks abdussamad, I had tried this already and for some reason, it doesn't always work for huge jumbled messes of words.

Here is the code, modified to clean out unnecessary characters:

PHP:
<?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>

Uploaded here:
http://www.regfee.info/php/clean.php

If I input

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.

I get
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

Which is what I want.

However, if I copy and paste the entire front page of Namepros, for example, I get things like this:
OutDomain
ServicesNP
Domain
RegistrationNP
Domain
ManagementBuy
Domain
CreditDomain
ToolsWHOIS
Lookup

and

2007
Jelsoft
Enterprises
LtdSearch
Engine
Friendly
URLs
by
vBSEO
240

Any idea why these two inputs would be treated any differently by the explode function?

Thanks again!
 
0
•••
One probably has more than 1 space. Consider using a regex-based explode that will break on whitespace instead of just spaces.
 
0
•••
Thanks

Thanks manaco and everyone else.

http://www.regfee.info/php/clean.php will now (hopefully) clean a big huge mess of words into words of specified length and will even tack on an extension if you'd like.

Here's the code (still needs to be cleaned up, but since I'm just starting out, I like to keep everything as unoptimized as possible to minimize my own confusion.)

PHP:
<?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>


Please test the script out if you get a chance and recommend any optimizations.

Dan, I would like to place this tool on regfee.info, if you don't mind. Also if any of you would like me to place a link to one of your sites at the bottom of the script, just let me know.

Thanks!
-Jorge
 
0
•••
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.
 
0
•••
Monaco's right; a regular expression match is far simpler, and can also include the min/max length checking.

Here's a modification of your php code from above (I didn't copy the html below it). The code also includes a check for two additional $_POST variables: 'numbers' and 'dashes', which modify the search filter to also include numbers and/or dashes.

You can add checkboxes for this (if you'd like) to your html form.

PHP:
<?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:';}
?>
 
Last edited:
0
•••
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.

Thanks Dan!

This is how I like to learn, by diving right into code. I try to crack open a beginner's php book, and I'm bored within 2 seconds.

Your suggestion worked:

PHP:
    <?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>


uploaded to http://www.regfee.info/php/fasterclean.php

Now I just need to display the results in a textarea for easier copy/pasting, and add a counter to display the number of generated domains.

I was thinking of making it check for duplicates as well, but won't that involve either huge arrays or using a db?

EDIT: Nevermind, right when I posted the question about duplicates, I realized I was being an idiot. I'm already running everything through huge arrays, so why not just use:
PHP:
$thing = array_unique($thing);

Perfect!
 
Last edited:
0
•••
Never mind, not so perfect after all. It's not removing duplicates, and since I've tried every variation of code I code find that mimics the unique array function (including one that checked if each value matched a new array and then added the value to the array if it didn't), but all of them keep failing to remove the duplicates. I have a strong feeling it is due to whitespace again, but I thought I took care of that with the ereg.

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?
 
Last edited:
0
•••
PHP:
...
        foreach ($thing as $word)
        {
                $word = trim($word);
 ...
 
0
•••
I figured out my mistake. I was trying to remove duplicates from the miniature arrays created with each loop, but not from the final product. I ended up making it so each word that meets the length requirements is put into a new array, then that new array is finally stripped of duplicates.

I then ran into the problem of different cases, and for some reason the array key case change wouldn't work, so I just did a strtolower($word) on the words before putting them in the new array.

My issue NOW is getting the new dashes and numbers thing to work with ereg, since I couldn't get preg to work.

PHP:
...

$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);

Almost there ;)
Jorge
 
0
•••
PHP:
<?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>


:gl:
 
0
•••
Last edited:
0
•••
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?

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)
 
0
•••
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)


Do you mind if I ask why it is necessary to add the [0] key in there? I already declared $stuff as an array, to me this seems like this would just be saying "make the first key of the array a word", not "make each key of the array a word."

Thanks,
Jorge
 
0
•••
When you do preg_match_all, it gives you two arrays (inside the result).
I forget what goes into each, but it's like the matches for (.*)? or whatever and then the full matched reg exp.
 
0
•••
Heh, as I was typing a response, Dan just posted!

When I wrote and tested the code I used different variable names, and then refactored them into what I pasted into my response. I guess I got a little overzealous when replacing and also removed the '[0]' part, which is why it was missing here but worked when I tested it.

Here's a new version, based on the last version (posted by Dan), with a couple of minor optimizations, a check and correction for min > max, and most importantly...sticky form values(!)

The form now remembers and displays all setting from the last call instead of resetting everything.

To do this, the code now generates the min/max size lists and extention list programmatically, so that it can easily assign the "checked" attribute to the relevant list items.

Also, this makes it easier to add or remove extentions, or change the min/max sizes.

Tested and works :)

PHP:
<?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>
 
Last edited:
0
•••
Thanks guys!
Cef, my next goal was to make the form remember the settings. I didn't think to make functions to do it, definitely a lot cleaner than I would've made it. Of course, now I have to think of something else to learn how to do, thanks ;)

Your guys' help has been invaluable. Maybe in a year I'll be able to help people who are just starting out as much as you've helped me.
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back