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
https://forums.phpfreaks.com/topic/267323-filter-data-from-one-input/
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?

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>

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.

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.

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>

Archived

This topic is now archived and is closed to further replies.

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