【问题标题】:i want to convert this function in to the Php function我想将此函数转换为 PHP 函数
【发布时间】:2015-07-21 20:34:25
【问题描述】:
private void CharsAvailable()
        {
            int num = 0;
            int num2 = 0;
            string text = this.txtMessage.Text;
            if (this.chkSignature.Checked)
            {
                text = text + Environment.NewLine + this.txtSignature.Text;
            }
            else
            {
                text = this.txtMessage.Text;
            }
            int num3 = 0;
            while (num != 1 && num3 < this.txtMessage.TextLength)
            {
                if (text[num3] < '0' || text[num3] > '9')
                {
                    if (text[num3] < 'A' || text[num3] > 'Z')
                    {
                    }  
                 ..............................
                   SOME CODE 
                 ............................

                if (num == 1)
                {
                    int num4 = text.Length;
                    if (num4 <= 70)
                    {
                        num4 = 1;
                    }
                    else
                    {
                        num4 += 62;
                        num4 -= num4 % 63;
                        num4 /= 63;
                    }
                    frmCompose.isUnicodeFound = true;
                    this.lblChar.Text = string.Concat(new object[]
                    {
                        text.Length,
                        " characters, ",
                        num4,
                        " SMS message(s)"
                    });
                }
                else
                {
                    int num4 = text.Length + num2;
                    if (num4 <= 160)
                    {
                        num4 = 1;
                    }
                    else
                    {
                        num4 += 152;
                        num4 -= num4 % 153;
                        num4 /= 153;
                    }
                    frmCompose.isUnicodeFound = false;
                    this.lblChar.Text = string.Concat(new object[]
                    {
                        text.Length + num2,
                        " characters, ",
                        num4,
                        " SMS message(s)"
                    });
                }
            }

            //validation

                        if (this.txtMessage.Text.Trim().Length == 0)
                        {
                            MessageBox.Show("Blank messages cannot send. Please type the message.", "Blank Message");
                        }
                        else if (frmCompose.isUnicodeFound && this.cmbLanguage.SelectedIndex == 0)
                        {
                            MessageBox.Show("Please choose 'Unicode' in Language dropdown to send message(s) in non English.”", "Unicode Message");
                        }enter code here

我需要一些帮助来将此函数转换为 php 函数。 php代码中相关函数中的类似语法或类似函数是什么?任何人都可以提供帮助。如果我得到一些需要进行的更改列表,那就太好了。在此先感谢。

【问题讨论】:

  • 所有 if 语句之母:D
  • 什么鬼?!为什么 1000 次相同的条件?文本[num3] != '?'为什么不只增加和检查其他情况?
  • 根据经验,任何超过 7 的嵌套都应该重构。
  • @Abhicenation,首先发布一个关于如何简化深层嵌套 if 语句的问题。
  • 它并不复杂,它很糟糕。有更好的方法来检查字符是否有效。看看正则表达式什么的。

标签: java php function


【解决方案1】:

首先,我强烈建议在将其转换为 PHP 之前重构您的代码。这只需通过两个数组并在检查字符时对它们进行循环来完成。这将替换大部分语句。

一个例子(在 php 中):

 $characters = array("{", "}", "\\", "[", "]", "~", "|", "€");
    $text = array();
    $num = 0;
    $num2 = 0;
    $num3 = 0;

    foreach($characters as $char)
    {
        if($text[$num3] == $char  ) 
        {
            $num2++;
        }else
        {
            $num = 1;
        }
    }

我试图保持当前代码的逻辑不变。仅实施较少过时/冗余。

现在我们只剩下前 3 个 if 语句了。这是正则表达式的完美候选。在 PHP 中使用 preg_match 可以轻松替换这 3 个 if 语句。

一个 PHP 示例:

    $text = "abcABC123";
    $regex = "/[a-zA-Z0-9]/";

    if(preg_match($regex, $text))
    {
        //Do stuff
    }

其余的代码相当简单,主要是逻辑。有一件事是肯定的,请研究如何更有效地实现某些事情。它可以帮助您和将来使用该代码的人:)

【讨论】:

    猜你喜欢
    • 2013-09-25
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    相关资源
    最近更新 更多