Convert function for next ASP to PHP

3

I have this function in ASP and need to convert to PHP

DATA:

cod = "01A" ou cod = "01B" ou cod = "01C"

titulo = "Novidade"

FUNCTION:

letra = right(cod,1)
str = array("A","B","C")
for i = lBound(str) to ubound(str)
if (instr(1,letra,str(i),vbtextcompare) <> 0) then
    titulo = titulo & "-" & i+2
end if
next

RESULT:

se cod="01A" então titulo = "Novidade-1"

se cod="01B" então titulo = "Novidade-2"

se cod="01C" então titulo = "Novidade-3"

Thank you all for the help

    
asked by anonymous 11.09.2017 / 10:32

2 answers

2

Look at what I understood from the question, do you want the letter to match an index? If so, see if this solution suits you:

 <?php
  $letras = range('A', 'Z');
  $cod = '10F';
  $titulo = "Novidade";

  $letra = substr($cod,-1);
  $index = array_search($letra, $letras)+1;

  print "$titulo-$index\n";

Of course, I've extrapolated a bit from your result, but the end result would be this:

Novidade-6
    
11.09.2017 / 16:38
3

As the goal is to compare the value of letra with the current item of the array str(i) .

One way to do this with php is to create a template through an array where the keys are the letters (AZ) and the values are 1-26 the one that performed this task is array_merge() which receives two arguments o first are the keys of the new array and the second are the values. After that, just check if there is a% index of% in% with% positive if you type% with% followed by value.

$arr = array('01A', '01B', '01C', '01Z');
$titulo = 'novidade';
$str =  array_combine(range('A', 'Z'), range(1,26));


foreach($arr as $v){
    $letra = substr($v, -1, 1);
    if(isset($str[$letra])){
        echo 'novidade-'.$str[$letra] .'<br>';
    }   
}

Output:

novidade-1
novidade-2
novidade-3
novidade-26
    
11.09.2017 / 14:15