i want to delete duplicate string and original duplicate!
for example:
my string = one two three one two
and i want = three
my code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<textarea name="keywords" rows="20" columns="120"></textarea>
</p>
<p>
<input type="submit" name="submit" />
</p>
</form>
<?php
if(!empty($_POST['keywords']))
{
$posted = $_POST['keywords'];
$posted = array_unique(explode(' ', str_replace("\r\n", ' ', $posted)));
echo print_r($posted, true);
}
?>
please Help me
Thanks
After you explode your string to words - count all values in array:
$posted = explode(' ', str_replace("\r\n", ' ', $posted));
$counted_values = array_count_values($posted);
// then filter by value, if value equals 1 - echo it, or do whatever you want
foreach ($counted_values as $k => $v) {
if ($v == 1) {
echo $k;
}
}
Similar to the other answer but using array_filter:
$posted = array_filter(array_count_values(str_replace("\r\n", ' ', $posted)),
function($v) { return $v === 1; });
Related
How do i get the values when submitted
I am generating the input via a loop based on the users selection but don't know how to retrieve the input values via post method
here is a sample of what i have
// string is based on database values it can be anything which i can't tell
Example code
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach($exp as $value){
print '<input type="text" name="'.$value.'[]" value="" />
}
You don't have to use name array (name="blabla[]")
$string = 'math,english,biology';
$exp = explode(',', $string);
if ($_POST) {
foreach ($exp as $name) {
if (isset($_POST[$name])) {
echo 'input ' . $name . ' is ' . $_POST[$name] . '<br>';
}
}
exit();
}
echo '<form method="post">';
foreach($exp as $value){
print '<input type="text" name="'.$value.'" value="" />';
}
echo '<button type="submit">Submit</button></form>';
Enter a, b, c to each input and submit. Here is the result:
input math is a
input english is b
input biology is c
Put the value in value="", name the field and make it an array [].
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $value) {
echo '<input type="text" name="fieldName[]" value="<?= htmlentities($value) ?>" />
}
Then it will be accessible in *$_POST['fieldName'] as an array.
*presuming you are using method="POST" on the form
If math,english,biology are form keys, then do:
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $key) {
echo '<input type="text" name="fieldName[<?= htmlentities($key) ?>]" value=""/>
}
or
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $key) {
echo '<input type="text" name="<?= htmlentities($key) ?>" value=""/>
}
I have a variable which renders a list of usernames. Now, I need to add a link to each username. The link is built from a fixed URL and the username as a variable.
Example link:
https://www.example.org/something?userid=$username
$usernames = $_POST['username'];
$username = '';
foreach($usernames as $value) {
$username .= "'" . $value . "', ";
}
$username = rtrim($username, ', ');
And then, I have the construction of the list:
<?php $urls = explode(',', $usernameid);
$nritems = count ($urls);
$positem = 0;
foreach($urls as $key => $usernameid)
{
echo "<a href="https://www.example.org/something?userid=' . $usernameid .
'">'. $usernameid . '</a>";
if (++$positem != $nritems) { echo ", ";}
}?>
The variable is also used in other parts of the code, so I can't change it. The list is not displaying now.
Any help is appreciated.
Update: the form:
<form action="list.php" method="post" autocomplete="off">
<div id="usrselect">
<label>Select user <input type="text" name="username[]" id="userid" class="typeahead" required/></label>
</div>
<div class="button-section"> <input type="submit" name="List" /></div>
</form>
If you just want to get your quotes right:
echo ''.$usernameid.'';
or
echo "{$usernameid}";
or
echo sprintf('%d', $usernameid, $usernameid);
The working solution, for those who might face the same problem:
<?php $urls = explode(',', $usernameid);
$nritems = count ($urls);
$positem = 0;
$displayuser = array("'", " ");
foreach($urls as $key => $usernameid)
{
echo ''.str_replace($displayuser, "", $usernameid).'';
if (++$positem != $nritems) { echo ", ";}
}?>
<form id="form1" name="form1" method="post" action="">
<input name="txt1" type="text" /><br />
<input name="txt2" type="text" /><br />
<input name="txt3" type="text" /><br />
<input name="s" type="submit" value="Find No"/>
</form>
Here is my html code.
<?php
$a = 10;
$b = 25;
$c = 20;
if($a > $b)
{
if($a > $c)
{
echo "a is biggest number";
}
else
{
echo "c is biggest number";
}
}
if($b > $c)
{
echo "b is biggest number";
}
?>
` Here is the code for finding
largest among three no.s . I wish to get largest and second largest no
Given an array of numerical strings obtained from the input fields, for example one that mirrors your original values:
$a = array('1', '5', '19', '200', '999');
you can convert it into a true numerical array using the following approach:
$a = array_map('intval', $a);
and then proceed exactly as you did before:
$last = max($a);
$second = max(array_diff($a,[$last]));
Fiddle here.
you have to use input box for this and a submit button for very simple basic working model.
HTML:
<form action="/path to your php file or leave blank if both html and php are in same file">
<input type="text" name="arr" value=''>
<input type="submit" name="submit" value="submit">
</form>
// now user can provide input with space in input box such as 9 11 13 15 16 29 etc.
PHP
<?php
if(isset($_REQUEST['arr']) && !empty($_REQUEST['arr'])){
$a = explode(' ', $_REQUEST['arr']);
$last = max($a);
$second = max(array_diff($a,[$last]));
echo $second;
echo $last;
?>
I think this will resolve your query.
Here we use reverse array sort with largest and second largest number
$result = array(1,5,19,200,999);
$count = count($result);
for($i=0;$i<$count;$i++){
for($j=$i+1;$j<$count;$j++){
if($result[$i]<$result[$j]){
$m = $result[$i];
$result[$i] = $result[$j];
$result[$j] = $m;
}
}
}
echo "Largest Number : ".(!empty($result[0])?$result[0]:FALSE)." Largest Second Number : ".(!empty($result[1])?$result[1]:FALSE);
I have the following issue:
I can't search multiple words.
I have a search engine who search just the full string:
PHP Code:
function ft_search_sidebar() {
$sidebar[] = array(
"id" => "search_1",
"content" => '<div class="section">
<h2>'.t('Search files & folders').'</h2>
<form action="" method="post" id="searchform">
<div>
<input type="text" name="q" id="q" size="16" value="'.$_REQUEST['q'].'" />
<input type="button" id="dosearch" value="'.t('Search').'" />
</div>
<div id="searchoptions">
<input type="checkbox" name="type" id="type" checked="checked" /> <label for="type">'.t('Search only this folder and below').'</label>
</div>
<div id="searchresults"></div>
</form>
</div>'
);
return $sidebar;
}
function ft_search_ajax($act) {
if ($act = '%search%') {
$new = array();
$ret = "";
$q = $_POST['q'];
$type = $_POST['type'];
if (!empty($q)) {
if ($type == "true") {
$list = _ft_search_find_files(ft_get_dir(), $q);
} else {
$list = _ft_search_find_files(ft_get_root(), $q);
}
Can someone help me?
Grtz
You will need to either develop a search engine which is capable of multi keyword search or simply extract out all the keywords from the search string and then use the search engine multiple times for each keyword. Like this:
$q = $_POST['q'];
$type = $_POST['type'];
if (!empty($q)) {
if(strpos(trim($q),' ') > 0)
{
$array = explode(' ',trim($q));
foreach($array as $key=>$value)
{
//process search multiple times for different values of $value
}
}
else
{
if ($type == "true")
{
$list = _ft_search_find_files(ft_get_dir(), $q);
}
else
{
$list = _ft_search_find_files(ft_get_root(), $q);
}
}
i have the following PHP and HTML code,
<?PHP
$filename = 'txt.txt';
$str = file_get_contents($filename);
$data = explode("\n", $str);
$data2 = explode(":", $data[0]);
$data3 = explode(":", $data[1]);
$first_name = $data2[1];
$age = $data3[1];
?>
<html>
<body>
name:<input id="abc1" onload="InputValidate1()" value="<?php echo $first_name ?>" > </input>
age:<input id="abc" onload="InputValidate()" value="<?php echo $age ?>" > </input>
</body>
</html>
and the text file 'txt.txt' contents are:
name:jon
age:25
i get the output as above,
but i want to get the same output with following contents in the text file,
-----details-----
name:jon
----cont----
age:25
i.e. i want to ignore those lines in between and display only the useful contents, how to read/ignore the contents of a single line in php ? or is there any other way to achieve this ?
You could use preg_match to match lines that begin with "-" and unset them from array.
<?php
$filename = 'txt.txt';
$str = file_get_contents($filename);
$data = explode("\n", $str);
foreach ($data as $key => $line) {
if(preg_match('/^-/', $line)){
echo "match $line";
unset($data[$key]);
}
}
$data2 = explode(":", $data[1]);
$data3 = explode(":", $data[3]);
$first_name = $data2[1];
$age = $data3[1];
?>
<html>
<body>
name:<input id="abc1" onload="InputValidate1()" value="<?php echo $first_name ?>" > </input>
age:<input id="abc" onload="InputValidate()" value="<?php echo $age ?>" > </input>
</body>
</html>