Hello, I was wondering if anyone can tell me of any php functions to convert a string into a number. I looked around the php manual but there doesn't seem to be anything like that.

The reason I ask is I have some pages with a string as a variable in the URL:
www.mysite.com/index.php?var=string123
www.mysite.com/index.php?var=2string4

I want to implement the Google Friend Connect Review/Rate gadgets in the page. Since Google Friend connect gadgets need to have a unique ID in order to be used just within the scope of that one page, i wanted to grab the value of $_GET['var'] and convert that into a decimal number.

I tried a function than converts ascii to binary but the Google Friend Connect ID number seems to have to be a decimal number.

Any suggestions?
Thanks

    [man]intval[/man]? Or just cast it? How should "string123" be converted to a number? How should "I've got a lovely bunch of coconuts" be converted to a number?

      I think this is along the lines of what you are looking for.
      I'm a bit rusty on the sprintf function, so that part might need tweeking.
      $string represents the string you want to make into a number, and $decimal specifies how many decimal places it should have.

      <?php
      function toNumber ($string, $decimal = 0) {
      	$decimal = (int)$decimal;
      	if (!is_numeric($string))
      		return 0;
      	$formatted = sprintf('%01.'.$decimal.'f', $string);
      	if ($decimal > 0)
      		return (double)$formatted;
      	else
      		return (int)$formatted;
      }
      ?>

      Altough, this might be easier:

      $variable = (double)$_GET['arg'];

      EDIT
      I misread the first post.
      You need to regex your string for numeric values

        What do you mean by "just cast it"?
        I need to be able to convert a string to a value that can be converted back into the same string later if need be. I was wondering if there is something like base64_encode that generates a deciml number
        I'm open to ideas
        Thanks

          ord() and then chr() perhaps? but i think we are still lost on what you want to do, and more importantly why.

            I tried to explain...
            I want to embed a Google Friend Connect gadget/widget on some of my page but I have to have a unique ID for every gadget/widget and the ID has to be a number that has something to do with the page it is on. Since the pages I am putting it on don't have IDs in the URLs but have variables with strings made of letter and numbers, I have to convert that string into a numerical value.

            I am thinking of taking every letter of the string and convert it, say, the position of that letter in the alphabet and create a long string from those numbers... what do you think of that? if you know of a better way please tell me

              Why don't you just create new unique IDs for each? Why do you have to base it on the string?

                because there is already a lot of content and the content is dynamically created

                  after further research I realized that GFC does accept a text string as an ID so I won't need to write this function at all.
                  Thanks a lot anyways

                    still makes no sense marcnyc, but if you look at ord it will give you the ascii value (as an int) of a letter so you can jsut loop through them to get a id

                    --but no way this is the best option

                      Write a Reply...