string comparison in php - php

i have this function:
function GetListByKeywords($keywords)
{
$HOST_DB = "localhost";
$NAME_DB = "jobs";
$USER_DB = "root";
$PWD_DB = "";
$connect = mysql_connect($HOST_DB, $USER_DB, $PWD_DB);
$db = mysql_select_db($NAME_DB);
$table = split('[;]', $keywords);
$Log_query = mysql_query("SELECT id FROM employment") or die(mysql_error());
$p = 0;
while ($Res_user = mysql_fetch_array($Log_query)) {
$id[$p] = $Res_user;
$p++;
}
$Log_query = mysql_query("SELECT description FROM employment") or die(mysql_error());
$p = 0;
while ($Res_user = mysql_fetch_array($Log_query)) {
$responsabilite[$p] = $Res_user;
$p++;
}
$p = 0;
for ($i = 0; $i < sizeof($table); $i++) {
for ($j = 0; $j < sizeof($responsabilite); $j++) {
if (strcmp($table[$i], $responsabilite[$j]) > 0) {
$marques[$p] = $id[$j];
$p++;
}
}
}
return $marques;
}
i want to compare the string $keywords to the fields of $responsabilite. so i convert it to the table $table and i compare each element by each element in the table responsabilite. so i need a function that can replace strcmp because i need to know if the element of $keywords exist in the string $responsabilite[$i] or not

Why don't you check if element in one array exist in other array with array_ intersect

Related

Make sure password contains at least one character from 3 arrays

How do I make sure that the password contains at least one character from each array? I could use do..while, right? What I don't quite understand is what I will need to put in the if statement inside do...while.
Is do...while the only way to ensure at least one character from each array is in the final password or are there other ways?
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$result = ""; //final random password
for($i = 0; $i < $length; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
echo $result;
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$resultArr[] = $a1[array_rand($a1)];
$resultArr[] = $a2[array_rand($a2)];
$resultArr[] = $a3[array_rand($a3)];
for($i = 0; $i < $length-3; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$resultArr[] = $values[$chosen][array_rand($values[$chosen])];
}
shuffle($resultArr);
$result = implode($resultArr); //final random password
echo $result;
You can surround the for wit ha do..while and in the condition check with preg_match() and a regex pattern that checks if at least one character is in the result:
<?php
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$result = ""; //final random password
do {
for($i = 0; $i < $length; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
} while(!(preg_match('/^(?=.*?[0-9])(?=.*?[a-z])(?=.*?[%*+?!]).{1,}/', $result)));
echo $result;
If the there's no coincidences with the pattern using preg_match, then will return false, in the do..while condition we check if one preg_match is false, if it is, do it again.
I made my answer general for any number of arrays.
I would use preg_match() for this, but for the question you asked, yes, you need to check each array separately:
$user_inputted_password = $_POST['user_inputted_password'];
$length = strlen($user_inputted_password); // user input password - you are capturing this somewhere, right
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$validate = array();
$result = ""; //final random password
function populate_validation_array($match_num, &$validate) {
if (!in_array($match_num, $validate)) {
$validate[] = $match_num;
}
}
$numeric_indexes = array(1, 2, 3);
$index_count = count($numeric_indexes);
for($i = 0; $i < $length; $i++){
for($j = 0; $j < $index_count; $j++) {
$numeric_index = $numeric_indexes[$j];
if (in_array($user_inputted_password[$i], ${"a" . $numeric_index})) {
populate_validation_array($numeric_index, $validate);
}
}
}
if (count($validate) === $index_count) {
$values = array();
for($i = 0; $i < $index_count; $i++) {
$values[] = ${"a" . ($i + 1)};
}
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
echo $result;

How to insert array if pass is value

<?php
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
for($i=0;$i<3;$i++){
if($test1== "pass"){
$num = 1;
}
if($test2 == "pass"){
$num = 2;
}
if($test3 == "pass"){
$num = 3;
}
echo $num;
$check[$i] = $num;
}
?>
value: test1="pass",test2="fail",test3="pass"
I want: $check(1,3)
Take only the "pass"
I want to put a value equal to "pass" in the array. The condition is that the value must be equal to "pass".
but test1,test2,test3 not array.
Presumably you are looking for this:
$test1 = "pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
for($i = 1; $i <=3; $i++) {
$t = 'test'.$i;
if(${$t} == 'pass')
$check[] = $i;
}
print_r($check);
Gives you:
Array
(
[0] => 1
[1] => 3
)
It's somewhat difficult to tell, but if so, this uses Variable Variables, cutting down on some script when using sequential variables.
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";
$num =array();
for($i=0;$i<3;$i++){
if($test1 == "pass"){
$num[] = 1;
}
if($test2 == "pass"){
$num[] = 2;
}
if($test3 == "pass"){
$num[] = 3;
}
}
print_r(array_unique($num));
You can do this like :
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
$num = '';
if($test1== "pass"){
$num .= '1,';
}
if($test2 == "pass"){
$num .= '2,';
}
if($test3 == "pass"){
$num .= '3,';
}
$num = trim($num,',');
$check = explode(',',$num);

Count array values of an associative array not working

I am trying to count the values of an associative array, but it isnt doing what I want it to.
I am pulling data from a database, as I need all the answers to a question. The answers in the database are under the fields answer1, answer2, answer3 etc, up to answer20.
What I am trying to do is count the number of fields that contain data, and ignore the fields that dont have any data.
Here is my code so far:
<?php
$results = $db->get_results("SELECT * FROM wellness_hra_questions WHERE category='general' AND level='1' AND gender='All' AND active='yes' ORDER BY id ASC");
foreach($results as $result){
$id = $result->id;
}
$answers = $db->get_results("SELECT answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, answer11, answer12, answer13, answer14, answer15, answer16, answer17 FROM wellness_hra_questions WHERE id=".$id."");
//$db->debug();
for( $i=1; $i <= 17; $i++) {
foreach($answers as $a_result){
$answer = 'answer';
$answer_id = $answer.$i;
$answer_id2 = $a_result->$answer_id;
$answer1 = $a_result->answer1;
$array1 = array('1'=>''.$answer1.'');
$answer2 = $a_result->answer2;
$array2 = array('2'=>''.$answer2.'');
$answer3 = $a_result->answer3;
$array3 = array('3'=>''.$answer3.'');
$answer4 = $a_result->answer4;
$array4 = array('4'=>''.$answer4.'');
$answer5 = $a_result->answer1;
$array5 = array('5'=>''.$answer5.'');
$answer6 = $a_result->answer6;
$array6 = array('1'=>''.$answer6.'');
}
if($answer1 == TRUE){
$newvalue1 = $array1;
}
if($answer2 == TRUE){
$newvalue2 = $array2;
}
if($answer3 == TRUE){
$newvalue3 = $array3;
}
if($answer4 == TRUE){
$newvalue4 = $array4;
}
if($answer5 == TRUE){
$newvalue1 = $array5;
}
if($answer1 == TRUE){
$newvalue6 = $array6;
}
$newarray = array_merge($newvalue1,$newvalue2, $newvalue3, $newvalue4, $newvalue5);
$count = count($newarray, COUNT_RECURSIVE);
echo $count;
}
?>
You need a code cleanup here - if what you want is a count, and the array_merge is ONLY for this purpose, this should do what I believe you would like to do:
for( $i=1; $i <= 17; $i++) {
foreach($answers as $a_result){
$answer_id = 'answer'.$i;
$answer_id2 = $a_result->$answer_id;
$thisCount = 0;
$newarray = array();
for($j = 1; $j < 7; $j++) {
$var = "answer" . $j;
$$var = ${"a_result->answer" . $j};
if (strlen($$var) > 0) {
$arrayID = "array" . $j;
$$arrayID = array((string)$j => $$var);// I don't think you need this but if you do included array_merge here
array_merge($newarray, $$arrayID);
$thisCount++;
}
}
echo $count;
print_r($newarray); // show the contents of the merged arrays
}// end foreach
}//end for

Making array after explode

I want to create an array from an input string. Before this code, I've tried explode, but the array remains length 1. Each string that I've tried is still one in array[0]. Here's my code so far:
public function word()
{
$kata = array($this->kal->getHasil());
if (!empty($kata)) {
$n = count($kata)
for ($i = 0; $i < $n; $i++) {
$imin = $i;
for ($j = $i; $j < $n; $j++) {
if ($kata[$j] < $kata[$imin]) {
$imin = $j;
}
}
$temp = $kata[$i];
$kata[$i] = $kata[$imin];
$kata[$imin] = $temp;
}
for ($i = 0; $i < $n; $i++) {
echo "$kata[$i] ";
}
}
}
public function tokenize()
{
$temp = $this->kal->getHasil();
$token = explode(" ", $temp);
return $token;
}
$hasil = $pp->tokenize();
for ($i = 0; $i < sizeof($hasil); $i++) {
$st = new stemming();
$hasil[$i] = $pp->singkatan($hasil[$i]);
$hasil[$i] = $st->stem($hasil[$i]);
$hasil[$i] = $pp->stopWord($hasil[$i]);
//echo "$hasil[$i] ";
$hb = new hitungBobot($hasil[$i]);
$hb->word();
}
How would I fix this?
You can use a globar var, see the code:
public function word(){ $kata = array($this->kal->getHasil());
global $output;
if(!empty($kata)){
$ar= count($kata)
$output += $ar;
}
public function tokenize() {
$temp = $this->kal->getHasil();
$token = explode(" ",$temp);
return $token;
}
$output = 0;
$hasil = $pp->tokenize();
for($i=0; $i<sizeof($hasil); $i++) {
$st = new stemming();
$hasil[$i] = $pp->singkatan($hasil[$i]);
$hasil[$i] = $st->stem($hasil[$i]);
$hasil[$i] = $pp->stopWord($hasil[$i]);
//echo "$hasil[$i] ";
$hb = new hitungBobot($hasil[$i]);
$hb->word();
}
echo $output;

array_merge() How can I add a 'range' of an array name

I have a $nr variable that as the number of arrays with the same name that i created in a previous function, something like this:
$var = 'sorteios_'.$nr;
$$var = array($sorteio_id);
I have it in a While function so it was created something like 3 arrays with the names:
$sorteios_1 , $sorteios_2 , $sorteios_3
And i want to add them inside an array_merge, so i have to use the $nr that says how many arrays with the same name were created.
$nr = 3;
i want that the final result looks something like this.
$result = array_merge($sorteios_1, $sorteios_2, $sorteios_3);
That's the whole function if you want to check it (it's not complete because of the problem i'm having):
function check_sorteios(){
global $db;
$id = $_SESSION['userid'];
$query1 = "SELECT * FROM sorteios WHERE userid = $id";
$result1 = $db->query($query1);
$count = $result1->rowCount();
if ($count == 0){ $sorteios = 0; echo "sem sorteios";}
else{
$numero = 0;
$sorteios = 0;
$nr = 0;
while($row1 = $result1->fetch()){
if ( $numero == $count ){ return 0;}
$numero++;
$sorteio_id = $row1['id'];
$query2 = "SELECT * FROM productos WHERE id = $sorteio_id";
$result2 = $db->query($query2);
while($row2 = $result2->fetch()){
$data = $row2['data'];
$titulo = $row2['titulo'];
if (strtotime($data) > time()){
if(!isset($$sorteio_id)){
$$sorteio_id = 1;
}
$nr++;
$var = 'sorteios_'.$nr;
$$var = array($sorteio_id);
}
}
}
}
$result = array_merge($sorteios_1, $sorteios_2, $sorteios_3);
$occurences = array_count_values($result);
print_r($occurences);
}
You could try to create a string containing the code executing array_merge of all your arrays and then pass it to the eval function (http://it1.php.net/manual/it/function.eval.php)...
Something like this:
$str="\$result=array_merge(";
for($i=1;$i<=$nr;$i++){
$str.="\$sorteios_$i,";
}
$str=substr($str,0,-1);
$str.=");";
eval($str);
Then in $result you have what you need.
Is there a reason you can't recursively merge?
if ($nr > 0) {
$result = $sorteios_1;
for ($i = 2; $i <= $nr; ++$i) {
$result = array_merge($result, ${'sorteios_'.$i});
}
} else {
// you might want to handle this case differently
$result = array();
}

Categories