Convert function for next ASP to PHP

Asked

Viewed 111 times

3

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

DICE:

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 your help

2 answers

3

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

One way to do this with php is to create a template through an array where the keys are the letters (A-Z) and the values are 1-26 who performed this task is array_merge() that takes two arguments the first are the keys of the new array and the second the values. After that just check if there is the Indice $letra in $str positive case writes novidade- followed by the 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>';
    }   
}

Exit:

novidade-1
novidade-2
novidade-3
novidade-26
  • rray, I thank you, I give you more details: The array contains only letters (complete alphabet from A to Z). COD are nna (2 numbers and 1 letter). The function compares the COD letter and increments the title to '-1' if it is letter A; '-2' if it is letter B so on. I hope I have clarified, qq doubt post. Thank you.

  • I forgot to inform the most important. Before the function has; letter=right(Cod,1) here is separated the letter of Cod for the function compare. sorry the fault.

  • @Geo then you compare only if the letters are equal and then make the increment?

  • that’s right. for example COD=23B the title is = novelty-2

  • @Geo edited the answer see if it’s correct now.

2


Look what I understood from the question, do you want the letter to correspond to an index? If it is this see if this solution meets you:

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

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

  print "$titulo-$index\n";

Sure I extrapolated a little bit of your result, but the end result would be this:

Novidade-6
  • almost. Cod is a single variable. Example: A $Cod='10F' variable has arrived. Then separates the letter F compares and adds -6 in the title. The function checks only one Cod. at a time. And the result is unique: Cod=10F will have novelty-6. Thank you

  • kkkkkkkkkkk, I get it... but this is easier... is gnt responds more comprehensive, to contemplate everything... but just remove from the array

  • Flavio did so and worked: $arr = array($Cod); I put the single variable inside the arr(array). But can you reduce it further? My reasoning is old, 70 years and 50 of outdated languages. Now that I’m converting Asp into php. I count on the understanding of colleagues. Thank you

  • Flávio e rray, I really appreciate the quick answers. The 2 suggestions worked perfectly. I will take the 2 tb for other functions. Thank you very much for your attention!!!

  • I tried to answer the 2 answers, but only accepts 1. Rray thank you for dedicating your time and knowledge. Good luck to all.

  • @Geo the important thing is to always continue learning, good luck. The community is always here to help you grow.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.