views:

94

answers:

3

Unfortunately I inherited some code (c/c++) that does some string manipulation and now I need to copy/port that over to php so this functionality can be accessed over the internets.

Specifically the functionality takes some arbitrary strings and "adds" them together. (the c code iterates down the character array and then does some checking to make sure they are in the alphanumeric range)

I can't find specific code examples on how to do this (I am not a PHP developer) - can anyone point me to some resources that will explain this? (basically how to do string/character array manipulation)

EDIT

In response to some comments and answers: I want the result in ascii, but essentially I will be adding base 36 numbers.

The C code right now converts to base 36 (from ascii) then "adds" each element together (does not carry - although the original author intended that - and it for some strange reason does the "add" from most significant to least) Then converts back to ascii. Strings can be of different lengths

Based on the current answers i think I have enough of what I need. It is always frustrating sometimes learning a new language - you know exactly what you want and you can do it in other languages, just not the one that is for the task at hand...

Thanks for the responses so far.

+3  A: 

If you have a string like this in php you can just call the index of an individual character like so:

<?
  $x = "Hello";
  print $x[0] . "\n";

So in other words, $string_var[n] gives you the nth char, 0-indexed.

Paul Huff
right, but then how to do the "addition" of the strings?
Tim
+1  A: 

First off, I'm assuming you want to add ascii values.

ord() might help you. Based on the other answer, something like:

<?php
function addStrings($x, $y){
  // Assumes that both strings are the same length
  for($i=0; $i<strlen($x); $i++){
    $result[i] = ord($x[i]) + ord($y[i]);
  }
  return $result;
}
?>

If you use this, you'll probably want to do something if $x and $y are different lengths, but I think it gets the idea across.

Brendan Long
Yes, in the c code they were converted to base 36, then converted back to ascii. thanks
Tim
+3  A: 

Can't you just base_convert() them?

$sum   = base_convert($str1, 36, 10) + base_convert($str2, 36, 10);
$sum36 = base_convert($sum, 10, 36);

Or do you need arbitrary precision? Here's a stab at arbitrary precision addition, in base 36:

function b36_add($str1, $str2)
{
    $to10 = array();
    for ($i = 0; $i < 36; ++$i)
    {
        $to10[base_convert($i, 10, 36)] = $i;
    }

    $len  = max(strlen($str1), strlen($str2));
    $str1 = str_repeat('0', $len - strlen($str1)) . $str1;
    $str2 = str_repeat('0', $len - strlen($str2)) . $str2;

    $pos   = $len - 1;
    $carry = 0;
    $sum   = '';

    do
    {
        $tmp    = base_convert($carry + $to10[$str1[$pos]] + $to10[$str2[$pos]], 10, 36);
        $sum   .= substr($tmp, -1);
        $carry  = (int) substr($tmp, 0, -1);
    }
    while (--$pos >= 0);

    $sum = strrev($sum);
    if ($carry)
    {
        $sum = base_convert($carry, 10, 36) . $sum;
    }
    return $sum;
}
Josh Davis
that looks like it would work for me. I am not that familiar with php. Thanks
Tim