The function reads two strings from file data.db (utf-8). The file contains strings: '123' and '123'. I checked it via ECHO and it displays the content correctly. Thus im sure the problem is not in the file.
When I try to match the values with the file the variable $access never change.
//got data from $_POST....
function au_check($login,$psw){
$f = file('data.db');
$l = $f[0];
$p = $f[1];
$access = 'fail';
if ($login==$l && $psw==$p){
$access='CHANGED';
}
return $access;
}
echo (au_check($_POST['login'],$_POST['pass'])); //returns FAIL :((((
BUT! If i change my values DIRECTLY in code IT WORKS...
//got data from $_POST....
function au_check($login,$psw){
$f = file('data.db');
$l = '123';
$p = '123';
$access = 'fail';
if ($login==$l && $psw==$p){
$access='CHANGED';
}
return $access;
}
echo (au_check($_POST['login'],$_POST['pass'])); //returns CHANGED.
?>
plz help! how to fix and what is wrong? that's so weird ....
file returns the lines, complete with newline character.
Use something like this to trim them off (the trim function is too heavy-handed):
$lines = file('data.db');
$f = array_map(function($line) {return rtrim($line,"\r\n");},$lines);
Related
I made a PHP script which is generating result "https://secure.nmi.com/api/v2/three-step/44505010"
44505010 is a number that changes every time. I need to create a $reference string which I can assign that dynamic number to $reference.
I tried this code but it doesn't seem to work. Can someone help me out?
$reference = getStr($page, 'https://secure.nmi.com/api/v2/three-step/','');
Thanks
Try this:
<?php
function getUrlPart($url = '') {
$urlarr = parse_url($url);
$split = explode('/', $urlarr['path']);
$c = 1;
while($split[count($split)-$c] == ''){
$c++;
}
return var_export($split[count($split)-$c], true);
}
$url = 'https://secure.nmi.com/api/v2/three-step/44505010//';
echo getUrlPart($url);
In this method you can get the route name at any depth, even with query parameters at the end.
$yourUrl = 'https://secure.nmi.com/api/v2/three-step/44505010';
$reference = (int) explode('/', $yourUrl)[6];
This assumes the general URL format does not change.
I meet a strange thing with the PHP function strpos().
I have a function that check if a passed string is found in a txt file.
I can display the content of the file line by line but the strpos() doesn't return a value (nothing in fact). var_dump() of the return empty.
Can someone see a mistake, because I am lost.
Thank you in advance.
My function :
function checkIfExist($string)
{
$path = "\\\\server\\temp\\test.txt";
$file = file($path);
foreach( $file as $line )
{
echo $line; //display the string in this line
$found = strpos($file,$string);
echo $found; //display nothing, not even a boolean/int
}
return $found;
}
Try to change $found = strpos($file,$string); to $found = strpos($line,$string);
Echoing a false boolean won't show up. Try changing it to a var_dump and you will see that it's a boolean set to false.
Sorry, I have made a mistake when writen the code, this is the good one :
function checkIfExist($string)
{
$path = "\\\\server\\temp\\test.txt";
$file = file($path);
foreach( $file as $line )
{
echo $line; //display the string in this line
$found = strpos($line,$string);
echo $found; //display nothing, not even a boolean/int
var_dump($found); //display boolena(false) for all the test even if the
string is well present once.
}
return $found;
}
This code give the same result
foreach( $file as $line )
{
echo $line; //display "www.google.be"
echo $string; //also display "www.google.be"
//but when I then if the line contain the string, the function doesn't find
it!!!
$pos = stripos($line,$hostname);
var_dump($pos); // FALSE for all the test
}
I have done this thes in other code, and I never had this issue.
Setup debugging, so you see the values of strpos. If debugging cannot be arranged than vardump $line and $string. You will probably get unexpected values. Also try avoiding typecasting-issues. Perhaps this will work better.
if (strpos($line,$string) != false){...}else{...}
I'm trying to create a function to pick up words from a text file randomly, and no one here poblema. The problem arises when I try to verify if the user correctly inserts the words. Unfortunately, I always get a negative answer. From what I understood when called, the function can not save the contents into the variable that naturally remains empty.
<?php
function random_word() {
$dictionary = "dictionary.txt";
$word = file($dictionary);
$n = 0;
while ($n < 2) {
$n++;
$randomword = array_rand($word);
echo $word[$randomword];
}
}
$a = random_word();
echo "-----------------";
echo $a;
?>
If I try to check the $a variable it tells me that it is NULL. I'm sure the problem is the function but I know PHP shortly and I'm struggling to find the error.
You need to return something. Not sure if you want to return a string or an array but your code seems to be made for string.
<?php
function random_word() {
$dictionary = "dictionary.txt";
$word = file($dictionary);
$n = 0;
while ($n < 2) {
$n++;
$randomword = array_rand($word);
$returner .= $word[$randomword] . " ";
}
return trim($returner);
}
$a = random_word();
echo "-----------------";
echo $a;
?>
i'm using a php function to return words instead of characters it works fine when i pass string to the function but i have a variable equals another variable containing the string and i've tried the main variable but didn't work
////////////////////////////////////////////////////////
function words($text)
{
$words_in_text = str_word_count($text,1);
$words_to_return = 2;
$result = array_slice($words_in_text,0,$words_to_return);
return '<em>'.implode(" ",$result).'</em>';
}
$intro = $blockRow03['News_Intro'];
echo words($intro);
/* echo words($blockRow03['News_Intro']); didn't work either */
the result is nothing
str_word_count won't work correctly with accented (multi-byte) characters. you can use below sanitize words function to overcome this problem:
function sanitize_words($string) {
preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u",$string,$matches,PREG_PATTERN_ORDER);
return $matches[0];
}
function words($text)
{
$words_in_text = sanitize_words($text);
$words_to_return = 2;
$result = array_slice($words_in_text,0,$words_to_return);
return '<em>'.implode(" ",$result).'</em>';
}
$intro = "aşağı yukarı böyle birşey";
echo words($intro);
I am creating a script that will locate a field in a text file and get the value that I need.
First used the file() function to load my txt into an array by line.
Then I use explode() to create an array for the strings on a selected line.
I assign labels to the array's to describe a $Key and a $Value.
$line = file($myFile);
$arg = 3
$c = explode(" ", $line[$arg]);
$key = strtolower($c[0]);
if (strpos($c[2], '~') !== false) {
$val = str_replace('~', '.', $c[2]);
}else{
$val = $c[2];
}
This works fine but that is a lot of code to have to do over and over again for everything I want to get out of the txt file. So I wanted to create a function that I could call with an argument that would return the value of $key and $val. And this is where I am failing:
<?php
/**
* #author Jason Moore
* #copyright 2014
*/
global $line;
$key = '';
$val = '';
$myFile = "player.txt";
$line = file($myFile); //file in to an array
$arg = 3;
$Character_Name = 3
function get_plr_data2($arg){
global $key;
global $val;
$c = explode(" ", $line[$arg]);
$key = strtolower($c[0]);
if (strpos($c[2], '~') !== false) {
$val = str_replace('~', '.', $c[2]);
}else{
$val = $c[2];
}
return;
}
get_plr_data2($Character_Name);
echo "This character's ",$key,' is ',$val;
?>
I thought that I covered the scope with setting the values in the main and then setting them a global within the function. I feel like I am close but I am just missing something.
I feel like there should be something like return $key,$val; but that doesn't work. I could return an Array but then I would end up typing just as much code to the the info out of the array.
I am missing something with the function and the function argument to. I would like to pass and argument example : get_plr_data2($Character_Name); the argument identifies the line that we are getting the data from.
Any help with this would be more than appreciated.
::Updated::
Thanks to the answers I got past passing the Array.
But my problem is depending on the arguments I put in get_plr_data2($arg) the number of values differ.
I figured that I could just set the Max of num values I could get but this doesn't work at all of course because I end up with undefined offsets instead.
$a = $cdata[0];$b = $cdata[1];$c = $cdata[2];
$d = $cdata[3];$e = $cdata[4];$f = $cdata[5];
$g = $cdata[6];$h = $cdata[7];$i = $cdata[8];
$j = $cdata[9];$k = $cdata[10];$l = $cdata[11];
return array($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l);
Now I am thinking that I can use the count function myCount = count($c); to either amend or add more values creating the offsets I need. Or a better option is if there was a way I could generate the return array(), so that it would could the number of values given for array and return all the values needed. I think that maybe I am just making this a whole lot more difficult than it is.
Thanks again for all the help and suggestions
function get_plr_data2($arg){
$myFile = "player.txt";
$line = file($myFile); //file in to an array
$c = explode(" ", $line[$arg]);
$key = strtolower($c[0]);
if (strpos($c[2], '~') !== false) {
$val = str_replace('~', '.', $c[2]);
}else{
$val = $c[2];
}
return array($key,$val);
}
Using:
list($key,$val) = get_plr_data2(SOME_ARG);
you can do this in 2 way
you can return both values in an array
function get_plr_data2($arg){
/* do what you have to do */
$output=array();
$output['key'] =$key;
$output['value']= $value;
return $output;
}
and use the array in your main function
you can use reference so that you can return multiple values
function get_plr_data2($arg,&$key,&$val){
/* do job */
}
//use the function as
$key='';
$val='';
get_plr_data2($arg,$key,$val);
what ever you do to $key in function it will affect the main functions $key
I was over thinking it. Thanks for all they help guys. this is what I finally came up with thanks to your guidance:
<?php
$ch_file = "Thor";
$ch_name = 3;
$ch_lvl = 4;
$ch_clss = 15;
list($a,$b)= get_char($ch_file,$ch_name);//
Echo $a,': ',$b; // Out Puts values from the $cdata array.
function get_char($file,$data){
$myFile = $file.".txt";
$line = file($myFile);
$cdata = preg_split('/\s+/', trim($line[$data]));
return $cdata;
}
Brand new to this community, thanks for all the patience.