【问题标题】:Subscript digits from a string字符串的下标数字
【发布时间】:2016-01-03 08:19:44
【问题描述】:

我想为字符串中的每个数字下标。

例如:

$str = '1Department of Chemistry, College of 2Education for Pure Science';

我想要的输出:

<sub>1</sub>Department of Chemistry, College of <sub>2<sub>Education for Pure Science

我从一个字符串中提取了所有数字:

//digits from string 
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

但是如何将下标效果应用于数字和打印字符串?

【问题讨论】:

    标签: php regex subscript


    【解决方案1】:

    我已经知道你已经接受了答案,但我仍然发布这个答案,因为我正在研究它,其次这可能对其未来的其他人有所帮助。

    <?php
    
    $str = '1Department of Chemistry, College of 2Education for Pure Science';
    
    $strlen = strlen( $str );
    $numbers = array();
    $replace = array();
    for( $i = 0; $i <= $strlen; $i++ ) {
        $char = substr( $str, $i, 1 );
        // $char contains the current character, so do your processing here
        if(is_numeric($char)){
            $numbers[] = $char;
            $replace[] = "<sub>".$char."</sub>";
        }
    }
    
    echo $str = str_replace($numbers, $replace, $str);
    
    ?>
    

    【讨论】:

      【解决方案2】:

      我假设你想要这样的东西

      $string = '1Department of Chemistry, College of 2Education for Pure Science';
      $pattern = '/(\d+)/';
      $replacement = '<sub>${1}</sub>';
      echo preg_replace($pattern, $replacement, $string);
      

      找到的数字将被替换为子标签中的本身。该示例取自 preg-replace 的 PHP 手册,您可以在此处找到 http://php.net/manual/en/function.preg-replace.php

      【讨论】:

      • 如果只匹配数字,那么无大小写标志 /i 有什么意义?
      【解决方案3】:
      <?php
      
      function subscript($string)
      {
          return preg_replace('/(\d+)/', '<sub>\\1</sub>', $string);
      }
      
      $input    = '1Department of Chemistry, College of 2Education for Pure Science';
      $expected = '<sub>1</sub>Department of Chemistry, College of <sub>2</sub>Education for Pure Science';
      $output   = subscript($input);
      
      if ($output === $expected) {
          printf('It works! %s', htmlentities($output));
      } else {
          printf('It does not work! %s', htmlentities($output));
      }
      

      【讨论】:

        【解决方案4】:

        你可以使用preg_replace:

        preg_replace( '!\d+!', '<sub>$0</sub>', $str );
        

        Demo

        【讨论】:

        • 谢谢!能详细回答一下吗?
        • 详细说明如何?如果你点击函数名的链接,它会从官方手册中解释用法。
        • 我想知道替换参数中的“$0”是什么?
        • 请参阅文档:replacement 可能包含 \\n 或(自 PHP 4.0.4 起)$n 形式的引用,首选后一种形式。每个这样的引用都将被第 n 个带括号的模式捕获的文本替换。 n 可以是 0 到 99,\\0 或 $0 指的是整个模式匹配的文本。左括号从左到右(从 1 开始)计数,以获得捕获子模式的数量。要在替换中使用反斜杠,它必须加倍(“\\\\” PHP 字符串)。
        【解决方案5】:

        这可能会有所帮助:

        $str = '1Department of Chemistry, College of 2Education for Pure Science';
        preg_match_all('!\d+!', $str, $matches);
        foreach($matches[0] as $no){
            $str = str_replace($no, '<sub>'.$no.'</sub>', $str);
        }
        echo htmlentities($str);
        

        会给出输出:

        <sub>1</sub>Department of Chemistry, College of <sub>2</sub>Education for Pure Science
        

        preg_replace 将给出相同的输出:

        $str = '1Department of Chemistry, College of 2Education for Pure Science';
        $str = preg_replace( '!\d+!', '<sub>$0</sub>', $str );
        echo htmlentities($str);
        

        【讨论】:

        • 不过,preg_replace 的效率要高得多。
        猜你喜欢
        • 2015-05-10
        • 2020-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-03
        • 1970-01-01
        • 2011-04-02
        相关资源
        最近更新 更多