Forum Moderators: coopster

Message Too Old, No Replies

Split string and get unique ID

a=1, b=2, etc.

         

divaone

11:55 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



hi all,

i am trying to assign a unique and constant id to a string and am not sure what the best method is. here is the synopsis:

my value = $myuniqueid
the string = $page
ex. $page = abcdef

a unique id would be attained by using simple number assignments --> a=1 b=2 c=3 so abcdef ='s 123456 (i have no database from which to gain a unique id). i need to return $myuniqueid to the page to use in a js on a php page.

please lead me in the right direction. i've tried str_split to get an array then importing the array into preg_replace_callback but its getting messy and of course not working yet :) would a simpler(?) loop into str_replace work? i have a feeling there's a simple way of doing this out there already and i don't need to reinvent the wheel.

tia,
divaone

Timotheos

12:57 am on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not knowing how you're using this you could possibly start a session and use the session ID.

i have no database from which to gain a unique id

How about storing a number in a file and incrementing it when needed?

httpwebwitch

2:52 am on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



please explain why you need to turn a string into a number. There is probably a better solution.

Your string substitution is a two-way cipher... it won't produce unique IDs if the strings are the same. if that's a problem, combine the ciphered string with mktime().
$id = mktime().mycipher($mystring);

an easy shortcut to a=1,b=3... is
$number = ord($letter)-ord("a").

pad your numbers with a 0 on the left so a=01, b=02. then 11 is "k", not "aa". is 222 "vb", or "bv"? 0222 or 2202 removes the ambiguity.

start the number with a 9 so the leading zero isn't ignored.

function mycipher($instring){
for ($i=0;$i<strlen($instring);$i++){
$outstr.=str_pad((ord(substr($instring),$i,1)-ord("A")),2,"0",STR_PAD_LEFT);
}
return "9".$outstr
}

divaone

12:36 pm on Mar 25, 2004 (gmt 0)

10+ Year Member



thank you both for the input.

the purpose for my unique id: i have a javascript inside my php-rendered page. the js needs a uniqueID (and i should have mentioned the ID needs to be numerical only *sorry*). inside the php script is where i need the ID.

the php script produces a link:
< a href="index.php?page=SOMEWORD>SOMEWORD</a...

and i need:
<script type='text/javascript'>alo[alo.length] = new ID($MYUNIQUEIDHERE,'SOMEWORD','index.php?page=SOMEWORD');</script>
< a href="index.php?page=SOMEWORD>SOMEWORD</a...
(unescaped the double quotes for demo purposes)

as this js uses cookies, the uniqueID should remain constant throughout all sessions. its ok if SOMEWORD is repeated many times on one page and case doesnt matter, but SOMEWORD cannot equal SOMEWORDS.

with all this in my head i hope i've put it to screen a bit clearer :) i'm trying the above example now. no luck just yet.

thank you

Timotheos

5:05 pm on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok I got it now... I've had my morning coffee.

Just a little cleanup to get it working.

<?php
function mycipher($instring){
$outstr = "";
for ($i=0;$i<strlen($instring);$i++) {
$outstr.=str_pad((ord(substr($instring,$i,1))-ord("A")),2,"0",STR_PAD_LEFT);
}
return "9".$outstr;
}

Good job httpwebwitch.