duanlujiaji10335 2017-08-02 18:50
浏览 31
已采纳

PHP在描述中的#之后获取字符[关闭]

How can I make each product identification number mentioned in the below description into variables, excluding the # pulling them from the description in PHP?

Two 9-dram clear styrene tubes with snap-on caps, 1 x 2-3/4″ I.D. (25.2 x 68mm), are supplied as containers. Additional or replacement tubes #8909 are available.

As a safety precaution, BioQuip recommends using the #1135Y HEPA filter with this aspirator.

Also see #1135X aspirator syringe bulb. It allows users to aspirate specimens without inhaling particulate matter.

  • 写回答

3条回答 默认 最新

  • drpp5680 2017-08-02 19:03
    关注

    One could use this regular expression (regex):

    /#(\w+)/
    

    along with preg_match_all() which performs a global search for data matching a regex, and stores what it finds in an array, as follows:

    <?php
    
    $str = "Two 9-dram clear styrene tubes with snap-on caps, 1 x 2-3/4″ I.D. (25.2 x 68mm), are supplied as containers. Additional or replacement tubes #8909 are available.
    
    As a safety precaution, BioQuip recommends using the #1135Y HEPA filter with this aspirator.
    
    Also see #1135X aspirator syringe bulb. It allows users to aspirate specimens without inhaling particulate matter.";
    
    $pat = "/#(\w+)/";
    if ( preg_match_all( $pat, $str, $matches )) {
      $popped = array_pop( $matches );
      list( $alpha, $beta, $gamma ) = $popped;
      echo "The IDs are as follows:
    
    $alpha
    $beta
    $gamma
    ";  
    }
    


    See demo

    This regex specifies a hashtag followed by parentheses to "remember" one or more word characters, i.e. numeric or alphabetical data. By enclosing this expression with parentheses, the hashtag will be excluded and the remaining data stored in $matches[1]. Once $matches contains data, you are done unless you wish to utilize $matches[1] to create three unique variables and optionally display their values.

    You could dispense with using a regex and use the following code with the above-indicated value of $str:

    <?php
    $ids=[];
    $a = explode("#",$str);
    array_shift( $a );
    $ids = array_map( function( $e ) {
                      return explode(" ",$e)[0];
                      },$a);
    
    print_r($ids);
    

    See demo

    By avoiding preg_match_all(), the second solution may offer the advantage of better performance. Alternately, you can split the array returned by explode() into a list that only includes relevant information, and then create an array of the list variables for array_map(), as follows:

    <?php
    
    $ids=[];
    list(,$a,$b,$c) = explode( "#",$str );
    $ids = array_map(function( $e ) {
                      return explode(" ",$e )[0];
                      },[ $a,$b,$c ]);
    
    print_r( $ids );
    

    See demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题