NameSilo

Need help for a simple PHP script

Spaceship Spaceship
Watch

Taboo

Established Member
Impact
0
I have been trying to make a very simple script that will allow me to paste a list of domains into a text box, and then above that enter a keyword. So that all domains containing the keyword I enter will be shown.

example:

input: hotels

textbox:
blahdomain.com
vacationhotels.com
hotelsvacation.com
blahdomain2.com
blahdomain3.com

and then after I press 'submit' it would return

vacationhotels.com
hotelsvacation.com


Can anyone help, I'm trying to use preg_match but I think I'm doing many things wrong and I'm very frustrated :(

Thnx!
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Try using stripos(string, keyword) instead. Check each domain individually and put any string where stripos() != FALSE into an array. Implode the array and output the results.
 
0
•••
Well coding this was fun! Enter your search string in the input box, and your list of domains in the textarea, one per line. It will print each one that contains your search string. :tu:

Code:
<form action=whatever.php method=post>

<input type=text name=searchstring /> 
<br />
<textarea name=domains> </textarea>
<br />
<input type=submit name=submit value=Submit />

</form>



<?php

if ($_POST['submit']) {
  
$searchstring = $_POST['searchstring'];
$domains = $_POST['domains'];

$domains = explode("\n", $domains);
$amount = count($domains);


$i = 0;

	while ($i <= $amount) {
	$domain = $domains[$i];

		if (strpos($domain, $searchstring)) {
  		echo "$domain <br />";
		}

$i++;
}
  

  
}

?>
 
0
•••
Hmm, shorty yours may work but if I'm brutally honest I'm not a fan of how it's coded.

This is how i would do it:

PHP:
<?php

if(strlen($_POST['submit']) > 0)
{
	
	$domains 	= $_POST['domains'];
	$search 	= $_POST['search'];
	
	foreach(explode("\n", $domains) as $value)
	{
		if(eregi($search, $value))
		{
			echo $value . '<br />';
		}
	}
}

?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
	<textarea name="domains"></textarea><br />
	Search string: <input type="text" name="search" value="" /><br />
	<input type="submit" name="submit" value="submit" />
</form>

Matt
 
Last edited:
0
•••
A couple of small recommendations:

  1. You should safe-encode all the strings you display in the form that came from the textarea. change

    PHP:
    echo "$domain <br />";
    to
    PHP:
    echo htmlentities($domain), '<br />';
  2. The Critic is right; stripos is much faster than the *reg functions. Shorty's code uses strpos, which is case sensitive. For flexibility and convenience, use the insensitive version that Critic recommended. change
    PHP:
    if (strpos($domain, $searchstring))
    to
    PHP:
    if (stripos($domain, $searchstring))
 
0
•••
Thank you, you guys rock!
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back