today i have a problem with two strings.
The first comes from an Function-Parameter, the second string i'm getting from a file.
I only see these characters: "703866200".
When i convert the two strings in ASCII-Chars, get this (I have the characters separated by dots):
My Function-Param: 55.48.51.56.54.54.50.48.48
String from File: 55.0.48.0.51.0.56.0.54.0.54.0.50.0.48.0.48
This means that the two strings are detected always different (strcmp).
Can anybody help me? - Is it an Encoding Problem, or what means that the second String is filled with zeros?
BTW, my string2ascii function:
function string_to_ascii($string){
$ascii = NULL;
for ($i = 0; $i < strlen($string); $i++){
$ascii .= ".".ord($string[$i]);
}
return($ascii);
}
Related
This question already has answers here:
Generating (pseudo)random alpha-numeric strings
(18 answers)
Closed 5 years ago.
I'm trying to make some random string in PHP with 5 letters/numbers.
It's working fine but sometimes I get a shorter string.
$characterset='ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$count=0;
$lenght=strlen($characterset);
$i=5; #number of characters
while($count < $i){
$num=rand(1,$lenght);
$letter=substr($characterset, $num, 1);
$string.=$letter;
$count++;
}
And strlen($string) is sometimes 4 (checked 100 records, 85 was 4 characters)
String characters, like arrays, start counting from zero. Run your code a bunch of times: in addition to sometimes getting not enough characters, notice how you never get an A in there?
$num = rand(0,$lenght-1); will do it.
As an alternative method, you could do this:
$max_b36 = str_repeat("Z",$number_of_characters);
$max_dec = base_convert($max_b36,36,10);
$rand_dec = rand(0,$max_dec);
$rand_b36 = base_convert($rand_dex,10,36);
$result = sprintf("%0".$number_of_characters."s",$rand_b36);
This method uses (potentially) big numbers though, so it will only support 5 characters (on 32-bit systems) or 12 characters (on 64-bit systems).
Personally I'd replace your entire logic with this one line wonder :
print $characters = substr(
str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'),
0,5)
);
(slightly off formatting so it fits on the screeen)
Thank you all for help, following code working great:
$characters='ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$string = '';
$random_string_length = 5;
$max = strlen($characters) - 1;
for ($i = 0; $i < $random_string_length; $i++) {
$string .= $characters[mt_rand(0, $max)];
}
Is it possible to avoid the same strings? I generated 50 000 records and had 48 the same.
I'm a junior PHP developer.
For a costumer I need to place some strings into a pdf. I'm using FPDI and I like it.
I have an existing template PDF and I need to insert every characters of a string into a little graphic box (see image).
Every characters must have 2 millimeters (8px approximately) from each others.
Every strings can have different length, so I thought do like this:
$name = 'namenamename';
$stringcount = strlen($name)-1;
$countspace = $stringcount*2;
//121 = coordinate of first box
for ($x=121; $x <= $x+$countspace; $x = $x+2) {
for ($i=0; $i <= $stringcount; $i++) {
$pdf->SetXY($x, 37);
$pdf->Write(0,$name[$i]);
}
}
That doesn't work. This is the error:
Maximum execution time of 30 seconds
Can you help me please with the correct approach and with good explanation for a newbie? :)
Try this code:
$name = 'namenamename';
$string_length = strlen($name);
$coordinate = 121; //Give to the variable coordinate the beginning value, in this case 121
for ($i=0; $i < $string_length; $i++){ //make only one loop for the string length so the loop ends when there is no more characters
$char = substr($name,$i,1); // this is "the tricky part", with substr you can grab each character with its position in the string
$pdf -> SetXY($coordinate, 37); // here you put the coordinate for the character
$pdf -> Write(0, $char); // write it
$coordinate += 2; // and increment it by two, since the character are two spaces away from each other
}
hope that will help..
Maybe not a great solution but you can modify the execution time with this line of code
set_time_limit ( $seconds );
Anyway give it a try but i think that is more an error in the logic of the loop maybe.
Can you say exactly the coordinate where you need the two first characters, the first is 121 + something or 121?
I need to invent my own conversion function because of some encoding issue.
I decided that for now I will create a conversion table for my characters.
I want to know how I can possibly do an operation like the following C code to print out characters 'a' to 'z':
char a='a';
for(i=0;i<26;i++){
printf("%c",a);
}
How can I do that (incrementing characters value by value) in PHP ?
Variables containing characters can be incremented in PHP exactly as in C:
<?php
for ($i = 'a'; $i < 'z'; ++$i) echo $i;
Additionally, PHP allows incrementing of strings:
$x = "abc";
echo ++$x; # abd
You can also use range:
echo implode(range('a', 'z')); # abcdef....
Finally, you can convert between character and numeric ASCII index via chr and ord. respectively.
I have some strings containing alpha numeric values, say
asdf1234,
qwerty//2345
etc..
I want to generate a specific constant number related with the string. The number should not match any number generated corresponding with other string..
Does it have to be a number?
You could simply hash the string, which would give you a unique value.
echo md5('any string in here');
Note: This is a one-way hash, it cannot be converted from the hash back to the string.
This is how passwords are typically stored (using this or another hash function, typically with a 'salt' method added.) Checking a password is then done by hashing the input and comparing to the stored hash.
edit: md5 hashes are 32 characters in length.
Take a look at other hash functions:
http://us3.php.net/manual/en/function.crc32.php (returns a number, possibly negative)
http://us3.php.net/manual/en/function.sha1.php (40 characters)
You can use a hashing function like md5, but that's not very interesting.
Instead, you can turn the string into its sequence of ASCII characters (since you said that it's alpha-numeric) - that way, it can easily be converted back, corresponds to the string's length (length*3 to be exact), it has 0 collision chance, since it's just turning it to another representation, always a number and it's a little more interesting... Example code:
function encode($string) {
$ans = array();
$string = str_split($string);
#go through every character, changing it to its ASCII value
for ($i = 0; $i < count($string); $i++) {
#ord turns a character into its ASCII values
$ascii = (string) ord($string[$i]);
#make sure it's 3 characters long
if (strlen($ascii) < 3)
$ascii = '0'.$ascii;
$ans[] = $ascii;
}
#turn it into a string
return implode('', $ans);
}
function decode($string) {
$ans = '';
$string = str_split($string);
$chars = array();
#construct the characters by going over the three numbers
for ($i = 0; $i < count($string); $i+=3)
$chars[] = $string[$i] . $string[$i+1] . $string[$i+2];
#chr turns a single integer into its ASCII value
for ($i = 0; $i < count($chars); $i++)
$ans .= chr($chars[$i]);
return $ans;
}
Example:
$original = 'asdf1234';
#will echo
#097115100102049050051052
$encoded = encode($original);
echo $encoded . "\n";
#will echo asdf1234
$decoded = decode($encoded);
echo $decoded . "\n";
echo $original === $decoded; #echoes 1, meaning true
You're looking for a hash function, such as md5. You probably want to pass it the $raw_output=true parameter to get access to the raw bytes, then cast them to whatever representation you want the number in.
A cryptographic hash function will give you a different number for each input string, but it's a rather large number — 20 bytes in the case of SHA-1, for example. In principle it's possible for two strings to produce the same hash value, but the chance of it happening is so extremely small that it's considered negligible.
If you want a smaller number — say, a 32-bit integer — then you can't use a hash function because the probability of collision is too high. Instead, you'll need to keep a record of all the mappings you've established. Make a database table that associates strings with numbers, and each time you're given a string, look it up in the table. If you find it there, return the associated number. If not, choose a new number that isn't used by any of the existing records, and add the new string and number to the table.
I'm trying to convert a text string to hexadecimal in php (which sounds trivial enough) but all the conversions I have tried output incorrect data.
The string I need to convert is;
RTP1 •. • A ¥;¥9ÈKJ| %¯ : E~WF 3HxI#Y¥
The correct result is;
525450310120209501022e2095204120030503040ba53b03040ba539c84b041f4a7c1120202025af032020203a20457e0357462033487849230459a52020202020
But I consistently get;
52545031012020e280a201022e20e280a2204120030503040bc2a53b03040bc2a539c3884b041f4a7c1120202025c2af032020203a20457e0357462033487849230459c2a52020202020
The online calculator at http://www.swingnote.com/tools/texttohex.php works on this perfectly - I have emailed the author to request the php source code but have had no answer.
I've tried the following functions without success;
bin2hex($data);
function strToHex($string)
{
$hex='';
for ($i=0; $i < strlen($string); $i++)
{
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
for ($i = 0; $i < strlen($string); $i++) {
echo dechex(ord($string[$i]));
}
and a few others I can no longer find... I'm really at a loss with this so any help will be greatly appreciated!
Thanks!
Matthew
The input string appears to contain utf-8 encoded characters (I say this based on the output). Try converting these characters back into an ASCII/ISO-8859-1 alike format.
$indat = utf8_decode("...");
$hexdata = bin2hex($indat);
I usually just process it one char at a time.
$str = 'My Cool String!';
$hex = '';
$str_ary = str_split($str);
foreach($str_ary as $char)
{
$hex .= dechex(ord($char));
}
echo $hex;
Edit:
Looking at it again, it looks like our code is very similar (didn't notice the code :\ ). I believe Jeff Parker has the right idea in the comment, it might just be a display issue.