I have an application called mystique item where users has to guess the item that's behind the watermark. Water mark is revealing on time to time and everything is working perfect, but i have a "small" problem with guessing words. I have added words in arrays, separated with commas, and i'm exploding that array in my php, but for some reason, it only catches the first word as correct, everything else is being incorrect. Here's what i have done.
$gt = getVal('pics','gtext','online',1);
$won = getVal('pics','winner','online',1);
if($won=='no')
{
$counts = getGen(3);
$counts2 = getGen(4);
if($counts2==0)
{
$counts2 = 9999999999999;
}
$ccount = getCount2("$uid","$pid",date('Y-m-d H:i:s',$t1),date('Y-m-d H:i:s',$t2));
$ccount3 = getCount3("$uid","$pid");
if( $ccount>=$counts || $ccount3>=$counts2)
{
echo '4';
}
else
{
$sp = explode(",",$gt);
if(in_array($val, $sp)) // guess correct
{
echo '1';
}
else// guess wrong
{
echo '2';
}
}
}
gtext is the row where I store the words, my words has spaces in them, for example: new iphone,iphone 5s,apple ipad,etc etc).
And here's the code that checks the words:
$.post('guessit.php',{from:1,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d){
if(parseInt(d)==1){
$.post('guessit.php',{from:2,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d1){
advanced_example($('#uid').val(),'Congratulations!','You are the winner!!',1);
//setInterval($(location).attr('href','redirecttohome.php'),10000);
});
}else if(parseInt(d)==2){
$.post('guessit.php',{from:3,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d1){
advanced_example($('#uid').val(),'Wrong!','Please try again!',1);
//setInterval($(location).attr('href','redirecttohome.php'),10000);
});
}else if(parseInt(d)==3){
advanced_example($('#uid').val(),'Sorry!','Someone else was faster!',1);
//setInterval($(location).attr('href','redirecttohome.php'),8000);
}else if(parseInt(d)==4){
advanced_example($('#uid').val(),'Error!','You already attempted maximum times',1);
//setInterval($(location).attr('href','redirecttohome.php'),8000);
}
guessit.php is containing the first code I've showed you.
If you need anything else in order to help me, please let me know.
#AmalMurali What I need is next: I have in MySQL:
apple ipad,apple iphone4,apple ipod,iphone4,apple
I need them as strings as:
apple ipad
apple iphone4
apple ipod
iphone4
apple
You need to trim the whitespace for the if conditions to work as you want it:
$sp = explode(",",$gt);
$sp = array_map('trim', $sp); //trim all the elements in $sp
If the elements contain a whitespace in the beginning or end, the following condition will evaluate to FALSE, thus triggering the statements in else block:
if(in_array($val, $sp)) {
If the whitespace is removed, in_array should work and the code should work as expected.
Related
$word = "superman";
I would like to be able to randomly choose 2 letters from what ever word is in $word and build a fill in the blank box for me to be able to answer
example some how random picks the p and the a
su_erm_n comes up so I would fill in the first box with p and the second one with a I guess with a form
$wordfinished = "su" . $fillinblank1 . "erm" . $fillinblank2 . "n";
if ($wordfinshed == $word) {
echo "congrats";
}
else{
echo "try again";
}
I am just learning php I have some things that I have done that are very complicated but was having a hard time with the random stuff any help would help me learn this
You can use PHP's rand() function to select a random number from 0 to the length of the word you are modifying and change the character at that index to something else
For example:
$str = "superman";
$str[rand(0, strlen($str)-1)] = "_";
Assuming that the rand() function output a value of 3 for example, we'd end up with this output:
sup_rman
We can put this in a function that can be called more than once in order to make more than one blank space in the word:
function addBlank($str){
do{
$index = rand(0, strlen($str)-1);
$tmp = $str[$index];
$str[$index] = "_";
} while($tmp =! "_");
return $str;
}
This function accepts a string and after each call will replace a letter in the string with _. Each call will result in one more blank space. The variable $tmp holds the value of the string at the randomly chosen index and checks that it wasn't already a blank space, and if it was, it picks another index to try to replace.
So to put this in practice, we can call the above function multiple times and store the result back into a variable, here is example output:
$str = addBlank("superman");
//the value of $str is now sup_rman
$str = addBlank($str)
//the value of $str is now sup_r_an
$word = "superman";
$size = strlen($word) -1;
$fillinblank1 = $word[rand(0,$size)];
$fillinblank2 = $word[rand(0,$size)];
$wordfinished = "su" . $fillinblank1 . "erm" . $fillinblank2 . "n";
echo $wordfinished;
Output:
sueermun
Demo
http://ideone.com/Z66xsI
Just like the title of this post says, I would to be able to check if every letter of a word is found in another word. So far these are the lines of codes that I was able to come up with:
<?php
$DBword = $_POST['DBword'];
$inputWords = $_POST['inputWords'];
$inputCount = str_word_count($inputWords,1);
echo "<b>THE WORD:</b>"."<br/>".$DBword."<br/><br/>";
echo "<b>WORDS ENTERED:</b><br/>";
foreach($inputCount as $outputWords)
{
echo $outputWords."<br/>";
}
foreach($inputCount as $countWords)
{
for($i=0; $i<strlen($countWords); $i++)
{$count = strpos( "$DBword", $countWords[$i]);}
if($count === false)
{
$score++;
}
}
echo "<b><br/>TOTAL SCORE: </b>";
echo $score;
?>
My point in having the foreach with the $outputWords is to just output the letters entered.
As for the other foreach that has $countWords, I am using it to really check if all letters in the word entered are found in the $DBword. I am using the for loop to check every letter.
So far, I am not getting the output that I want and I just ran out of ideas. Any ideas please?
function contains_letters($word1, $word2) {
for ($i = 0; $i < strlen($word1); $i++)
if (strpos($word2, $word1{$i}) === false)
return false;
return true;
}
//example usage
if (contains_letters($_POST['inputWords'], $_POST['DBword']))
echo "All the letters were found.";
If this check should be case-insensitive (i.e. 'A' counts as a usage of 'a'), change strpos to stripos.
Since you are overwriting $count in the for loop for each letter in $countWords, $count will contain the position of the last letter of $countWord only. Also, I am not sure why you increase score when the letter wasn't found.
In any case, you are making your life more difficult than necessary.
PHP has a function for counting chars in a string:
return count_chars($dbWord, 3) === count_chars($inputWord, 3);
will return true if the same letters are found in both strings.
Example to find all the words having exactly the same letters:
$dbWord = count_chars('foobar', 3);
$inputWords = 'barf boo oof raboof boarfo xyz';
print_r(
array_filter(
str_word_count($inputWords, 1),
function($inputWord) use ($dbWord) {
return count_chars($inputWord, 3) === $dbWord;
}
)
);
will output "raboof" and "boarfo" only.
Sorry for the long title.
Wanted it to be as descriptive as possible.
Disclaimer : Could find some "find the differences" code here and elsewhere on Stackoverflow, but not quite the functionality I was looking for.
I'll be using these terminoligy later on:
'userguess' : a word that will be entered by the user
'solution' : the secret word that needs to be guessed.
What I need to create
A word guessing game where:
The user enters a word (I'll make sure through Javascript/jQuery that
the entered word contains the same number of letters as the word to
be guessed).
A PHP function then checks the 'userguess' and highlights (in green)
the letters in that word which are in the correct place, and
highlights (in red) the letters that are not yet in the right place,
but do show up somewhere else in the word. The letters that don't
show up in the 'solution' are left black.
Pitfall Scenario : - Let's say the 'solution' is 'aabbc' and the user guesses 'abaac'
In the above scenario this would result in : (green)a(/green)(red)b(/red)(red)a(/red)(black)a(/black)(green)c(/green)
Notice how the last "a" is black cause 'userguess' has 3 a's but 'solution' only has 2
What I have so far
Code is working more or less, but I've got a feeling it can be 10 times more lean and mean.
I'm filling up 2 new Arrays (one for solution and one for userguess) as I go along to prevent the pitfall (see above) from messing things up.
function checkWord($toCheck) {
global $solution; // $solution word is defined outside the scope of this function
$goodpos = array(); // array that saves the indexes of all the right letters in the RIGHT position
$badpos = array(); // array that saves the indexes of all the right letters in the WRONG position
$newToCheck = array(); // array that changes overtime to help us with the Pitfall (see above)
$newSolution = array();// array that changes overtime to help us with the Pitfall (see above)
// check for all the right letters in the RIGHT position in entire string first
for ($i = 0, $j = strlen($toCheck); $i < $j; $i++) {
if ($toCheck[$i] == $solution[$i]) {
$goodpos[] = $i;
$newSolution[$i] = "*"; // RIGHT letters in RIGHT position are 'deleted' from solution
} else {
$newToCheck[] = $toCheck[$i];
$newSolution[$i] = $solution[$i];
}
}
// go over the NEW word to check for letters that are not in the right position but show up elsewhere in the word
for ($i = 0, $j = count($newSolution); $i <= $j; $i++) {
if (!(in_array($newToCheck[$i], $newSolution))) {
$badpos[] = $i;
$newSolution[$i] = "*";
}
}
// use the two helper arrays above 'goodpos' and 'badpos' to color the characters
for ($i = 0, $j = strlen($toCheck), $k = 0; $i < $j; $i++) {
if (in_array($i,$goodpos)) {
$colored .= "<span class='green'>";
$colored .= $toCheck[$i];
$colored .= "</span>";
} else if (in_array($i,$badpos)) {
$colored .= "<span class='red'>";
$colored .= $toCheck[$i];
$colored .= "</span>";
} else {
$colored .= $toCheck[$i];
}
}
// output to user
$output = '<div id="feedbackHash">';
$output .= '<h2>Solution was : ' . $solution . '</h2>';
$output .= '<h2>Color corrected: ' . $colored . '</h2>';
$output .= 'Correct letters in the right position : ' . count($goodpos) . '<br>';
$output .= 'Correct letters in the wrong position : ' . count($badpos) . '<br>';
$output .= '</div>';
return $output;
} // checkWord
Nice question. I'd probably do it slightly differently to you :) (I guess that's what you were hoping for!)
You can find my complete solution function here http://ideone.com/8ojAG - but I'm going to break it down step by step too.
Firstly, please try and avoid using global. There's no reason why you can't define your function as:
function checkWord($toCheck, $solution) {
You can pass the solution in and avoid potential nasties later on.
I'd start by splitting both the user guess, and the solution into arrays, and have another array to store my output in.
$toCheck = str_split($toCheck, 1);
$solution = str_split($solution, 1);
$out = array();
At each stage of the process, I'd remove the characters that have been identified as correct or incorrect from the users guess or the solution, so I don't need to flag them in any way, and the remaining stages of the function run more efficiently.
So to check for matches.
foreach ($toCheck as $pos => $char) {
if ($char == $solution[$pos]) {
$out[$pos] = "<span class=\"green\">$char</span>";
unset($toCheck[$pos], $solution[$pos]);
}
}
So for your example guess/solution, $out now contains a green 'a' at position 0, and a green c at position 4. Both the guess and the solution no longer have these indices, and will not be checked again.
A similar process for checking letters that are present, but in the wrong place.
foreach ($toCheck as $pos => $char) {
if (false !== $solPos = array_search($char, $solution)) {
$out[$pos] = "<span class=\"red\">$char</span>";
unset($toCheck[$pos], $solution[$solPos]);
}
}
In this case we are searching for the guessed letter in the solution, and removing it if it is found. We don't need to count the number of occurrences because the letters are removed as we go.
Finally the only letters remaining in the users guess, are ones that are not present at all in the solution, and since we maintained the numbered indices throughout, we can simply merge the leftover letters back in.
$out += $toCheck;
Almost there. $out has everything we need, but it's not in the correct order. Even though the indices are numeric, they are not ordered. We finish up with:
ksort($out);
return implode($out);
The result from this is:
"<span class="green">a</span><span class="red">b</span><span class="red">a</span>a<span class="green">c</span>"
Here try this, See In Action
Example output:
<?php
echo checkWord('aabbc','abaac').PHP_EOL;
echo checkWord('funday','sunday').PHP_EOL;
echo checkWord('flipper','ripple').PHP_EOL;
echo checkWord('monkey','kenney').PHP_EOL;
function checkWord($guess, $solution){
$arr1 = str_split($solution);
$arr2 = str_split($guess);
$arr1_c = array_count_values($arr1);
$arr2_c = array_count_values($arr2);
$out = '';
foreach($arr2 as $key=>$value){
$arr1_c[$value]=(isset($arr1_c[$value])?$arr1_c[$value]-1:0);
$arr2_c[$value]=(isset($arr2_c[$value])?$arr2_c[$value]-1:0);
if(isset($arr2[$key]) && isset($arr1[$key]) && $arr1[$key] == $arr2[$key]){
$out .='<span style="color:green;">'.$arr2[$key].'</span>';
}elseif(in_array($value,$arr1) && $arr2_c[$value] >= 0 && $arr1_c[$value] >= 0){
$out .='<span style="color:red;">'.$arr2[$key].'</span>';
}else{
$out .='<span style="color:black;">'.$arr2[$key].'</span>';
}
}
return $out;
}
?>
When I read the alt(technically title)-text of this XKCD comic, I became curious whether every articles in Wikipedia eventually points to Philosophy article. So I began to make a web application that displays what articles it's "pointing" using PHP.
(PS: don't worry about traffic - because I'll use it privately and will not send too much requests to Wikipedia server)
To do this, I have to remove texts between parentheses and italics, and get the first link. Other things can be achieved using PHP Simple HTML DOM Parser, but remove texts between parentheses is the problem..
If there's no parentheses in parentheses, then I could use this RegEx:\([^\)]+\), however, like the article about German language, there's some articles have overlapped parentheses(for example: German (Deutsch [ˈdɔʏtʃ] ( listen)) is..), and above RegEx can't handle these cases, since [^\)]*\) finds first closing parentheses, not matching closing parentheses. (Actually above case doesn't become a problem since there's no text between two closing parentheses, but it becomes a big problem when there's a link between two closing parentheses.)
One dirty solution I can think is this:
$s="content of a wikipedia article";$depth=0;$s2="";
for($i=0;$i<strlen($s);$i++){
$c=substr($s,$i,1);
if($c=='(')$depth++;
if($c==')'){if($depth>0)$depth--;continue;}
if($depth==0) $s2.=$c;
}
$s=$s2;
However, I don't like this solution since it cuts down a string into single characters and that looks like unnecessary...
Is there other ways to remove text in a pair of(matching) parentheses?
For example, I want to make this text:
blah(asdf(foo)bar(lol)asdf)blah
into this:
blahblah
but not like this:
blahbarasdf)blah
Edit : from a comment of Emil Vikström's answer, I realized that above approach(remove texts between parentheses) may remove a link containing parentheses. However, I still want the answer of above problem, since I met similar problem before and I want to know the answer...
So my question is still: how to remove texts between matching parentheses?
You can check out recursive patterns, which should be able to solve the problem.
When I read the comic I didn't have the willpower to get my head around recursive patterns, so I simplified it to find a link and only then check if it's in parenthesis. Here's my solution:
//Fetch links
$matches = array();
preg_match_all('!<a [^>]*href="/wiki/([^:"#]+)["#].*>!Umsi', $text, $matches);
$links = $matches[1];
//Find first link not within parenthesis
$found = false;
foreach($links as $l) {
if(preg_match('!\([^)]+/wiki/'.preg_quote($l).'.+\)!Umsi', $text)) {
continue;
}else{
$found = true;
break;
}
}
Here's my entire script: http://lajm.eu/emil/dump/filosofi.phps
Great! I am seeing someone with a problem which I experienced while cleaning up Wikipedia plain text content. Here is how you use it.
cleanBraces("blah(asdf(foo)bar(lol)asdf)blah", "(", ")")
will return
blahblah
You can pass any type of braces. Like [ and ] or { and }
Here goes my source code.
function cleanBraces($source, $oB, $eB) {
$finalText = "";
if (preg_match("/\\$oB.*\\$eB/", $source) > 0) {
while (preg_match("/\\$oB.*\\$eB/", $source) > 0) {
$brace = getBracesPos($source, $oB, $eB);
$finalText .= substr($source, 0, $brace[0]);
$source = substr($source, $brace[1] + 1, strlen($source) - $brace[1]);
}
$finalText .= $source;
} else {
$finalText = $source;
}
return $finalText;
}
function getBracesPos($source, $oB, $eB) {
if (preg_match("/\\$oB.*\\$eB/", $source) > 0) {
$open = 0;
$length = strlen($source);
for ($i = 0; $i < $length; $i++) {
$currentChar = substr($source, $i, 1);
if ($currentChar == $oB) {
$open++;
if ($open == 1) { // First open brace
$firstOpenBrace = $i;
}
} else if ($currentChar == $eB) {
$open--;
if ($open == 0) { //time to wrap the roots
$lastCloseBrace = $i;
return array($firstOpenBrace, $lastCloseBrace);
}
}
} //for
} //if
}
The problem:
Let's say I have $keyword = a sentence entered into a search box, such as "large white boxes"
What I need to do is break this into individual words, and then test each word to make sure a * doesn't appear within the first 3 letters. (So, sen* would be ok, but se* would NOT be ok). If a * does appear in first 3 letters of any individual word, then the "if ($keyword) ..." process needs to end.
if ($keyword) {
$token = strtok($keyword, " ");
while ($token != false) {
echo $token;
if (stripos($token,"*") < 3 ) {
return;
}
$token = strtok(" ");
}
...code continues...
As you can see, I am echoing each time to see it processing.
If I get rid of the 'if' code, then it outputs 'largewhiteboxes' and continues on as expected.
If I leave the 'if' code as-is, only 'large' is output, and the routine ends - even though the condition has not been met!
If I run that 'if' statement on its own, outside of the WHILE loop, it works just fine, responding true to a * in first 3 positions, and false for everything else...
What might I be doing wrong with this???
There is a giant red warning in the documentation for stripos that you should heed.
In other words, you need to check if the return value !== false before checking if it is < 3.
As an aside, why do you bother with strtok when explode(',', $keyword) is available?
This variation seems to work.
$keyword = "large white boxes";
$token = strtok($keyword, " ");
while ($token !== false) {
echo $token;
$pos = stripos($token, "*");
if ($pos < 3 && $pos !== false) {
return;
}
$token = strtok(" ");
}