Jump to content

Filter data from one input


firstminister

Recommended Posts

Hi everyone..

 

sweet ppl, i have a long time, looking for a script which make this:

 

i need to filter, data of one input from the other one input! for example:

 

I write phone numbers (per line) on the first textarea, and on the other, i put phone numbers that i want to remove from the first textarea, and print results of cleaned numbers on the same page.. someone can help me please! sorry for english errors, im hispanic

Link to comment
Share on other sites

hahhaaha.. look my friend:

 

I have a Phone numbers list right, so, i want remove some numbers from that list... What is my idea? make a form with two input, in one input put the phone numbers list, and in the other one, put the numbers which i want to remove from that list..

 

u got it?

Link to comment
Share on other sites

Maybe this is a little over the top, creating arrays etc.

<?php
if (isset($_POST['listnumbers'])){
$listnumbers = nl2br(trim($_POST['listnumbers']));
$listnumbers = explode('<br />', $listnumbers);
$Newlist=array();
foreach ($listnumbers as $numbers){
	$numbers = str_replace("\r", "", $numbers);
	$numbers = str_replace("\n", "", $numbers);
	$numbers = preg_replace('~^(\<br /\>,/\n)+~', '', $numbers);
	$Newlist[] = $numbers;
}


$searchnumber = trim($_POST['searchnumber']);
if (in_array($searchnumber,$Newlist)){
	$number_key = array_search($searchnumber,$Newlist);
	unset($Newlist[$number_key]);
}
}
?>
<html>
<body>
<?php

echo "<pre>";
echo "<br />listnumbers<br />";
print_r($listnumbers);
echo "<br />Newlist<br />";
print_r($Newlist);
echo "<br />POST<br />";
print_r($_POST);
echo "</pre>";

?>
<form method="post" action="">
	<textarea name="listnumbers">
<?php 
echo (isset($Newlist)? implode('
', $Newlist):'');
?>
	</textarea><br />
	<textarea name="searchnumber"></textarea><br />
	<input type="submit" value="Check Number" />
</form>
</body>
</html>

Link to comment
Share on other sites

firstminister: Look into "array_unique ()" as that will do exactly what you're looking for.

 

Drummin: Why replace the newlines with br tags, only to explode on them? Much better to explode the newlines right away. Your code could easily have been reduced to three lines, using the array_unique () and stripping away all unnecessary operations.

Link to comment
Share on other sites

As pointed out, for single number removal a simple preg_replace will do the job.

<?php
if (isset($_POST['listnumbers'])){
$phone_number_list = trim($_POST['listnumbers']);
$phone_number = trim($_POST['searchnumber']);
$phone_number_list = preg_replace("/$phone_number/", "", $phone_number_list);
$phone_number_list = str_replace(PHP_EOL.PHP_EOL, PHP_EOL, $phone_number_list);
}
?>
<html>
<body>
<form method="post" action="">
	<textarea name="listnumbers"><?php if (isset($phone_number_list)){echo "$phone_number_list";} ?>
	</textarea><br />
	<textarea name="searchnumber"></textarea><br />
	<input type="submit" value="Check Number" />
</form>
</body>
</html>

If however if your needing to remove a list of numbers from another list of numbers this approach won't work and you will probably need to build an array for each input and then filter out matches within a foreach loop.

Link to comment
Share on other sites

This should do it.

<?php
if (isset($_POST['listnumbers'])){
$Newlist = explode(PHP_EOL, trim($_POST['listnumbers']));
$Searchlist = explode(PHP_EOL, trim($_POST['searchnumbers']));
$Newlist = array_diff($Newlist,$Searchlist);
$phone_number_list = implode(PHP_EOL,$Newlist);
}
?>
<html>
<body>
<form method="post" action="">
	<textarea name="listnumbers"><?php if (isset($phone_number_list)){echo "$phone_number_list";} ?>
	</textarea><br />
	<textarea name="searchnumbers"></textarea><br />
	<input type="submit" value="Check Number" />
</form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.