dsmvovm27249 2014-05-29 02:49
浏览 30
已采纳

如何将字符转换为大写,除了php中any()中的字符串?

I have a string like this:

abc(abc)(ABC)(abc)abc abc(abc)(ABC)(abc)abc

And I want to convert this into

ABC(abc)(ABC)(abc)ABC ABC(abc)(ABC)(abc)ABC

And there may be some character like this

$str= a+b*(a+b)<sup>2</sup>+something.
  • 写回答

1条回答 默认 最新

  • douju2331 2014-05-29 03:41
    关注

    You might be able to do it via REGEXP but that could have issues if there are compounded parenthesis... Something like this would work:

    $open = 0;
    $text = "abc(abc)(ABC)(abc)abc abc(abc)(ABC)(abc)abc";
    
    for ($i = 0; $i < strlen($text); $i++) {
        switch ($text[$i]) {
        case '(':
        case '<':
            $open++;
            break;
        case ')':
        case '>':
            $open--;
            break;
        default:
            if ($open === 0) {
                $text[$i] = strtoupper($text[$i]);
            }
        }
    }
    

    This is assuming you don't want to make your html tags uppercase, and that there isn't a mismatch in opening/closing parenthesis or angle brackets.

    If "$str=" could be part of the string, you could change the for loop to:

    for ($i = strpos($text, '=') ?: 0; $i < strlen($text); $i++) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊