代码之家  ›  专栏  ›  技术社区  ›  MIK

星号“*”的PHP正则表达式

  •  1
  • MIK  · 技术社区  · 7 年前

    我有一个以“*”或“^”结尾的字符串。
    例如:

    $variable = 'abcdef**^^';
    

    $variable = 'abcdef*^^^^';
    $words = preg_split("/(?<=\w)\b[!?.]*/", $variable, -1, PREG_SPLIT_NO_EMPTY);
    

    我得到的输出为

    Array
    (
        [0] => abcdef
        [1] => *^^^^
    )
    

    但是问题开始了,当我的变量被替换为@、“”和其他一些特殊字符时。
    例如:

    $variable = 'abc@d ef*^^^';  
    

    Array
    (
        [0] => abcd
        [1] => @ ef
        [2] => *^^^^
    )
    

    我需要这样的输出

    Array
    (
        [0] => abcd@ ef
        [1] => *^^^^
    )
    

    我试图用“*”和“^”的正则表达式替换当前的正则表达式,但找不到。

    4 回复  |  直到 7 年前
        1
  •  3
  •   Bilal Ahmed    7 年前

    使用 \*

    <?php
    
    $variable = 'abc@d ef*^^^';
    $words = preg_split("/\*/", $variable, -1, PREG_SPLIT_NO_EMPTY);
    echo "<pre>";
    print_r($words);  
    ?>
    

    它的输出是这样的

    Array
    (
        [0] => abc@d ef
        [1] => ^^^
    )
    

    如果你想添加*那么用这个

    <?php
    
    $variable = 'abc@d ef*^^^';
    $words = preg_split("/\*/", $variable, -1, PREG_SPLIT_NO_EMPTY);
    
    $i=0;  
    foreach ($words as $value) {
        if($i != 0)
        {
            $words[$i]='*'.$words[$i];
        }
        $i++;
    }
    
    echo "<pre>";
    print_r($words);
    ?>
    
        2
  •  1
  •   Shahbaz A.    7 年前

    “如果我掌握了问题的真正含义,”OP提到

    如果希望以这种方式拆分字符串,则应该这样更改regexp。

    $words = preg_split("/[\*|\^]/", $variable, 2, PREG_SPLIT_NO_EMPTY);  
    

    这将在*或^第一次并发时中断字符串。OP已经排除了一个答案,但我正在添加我的答案,以防在这种情况下对其他人有所帮助。

    编辑:

    $variable = 'abcd@ ef*';
    if ( strpos( $variable, '*' ) < strpos( $variable, '^' ) || strpos( $variable, '^' ) === false ) 
    {
        $first = strstr($variable, '*', true);
        $last = strstr($variable, '*');
    }
    if ( strpos( $variable, '^' ) < strpos( $variable, '*' ) || strpos( $variable, '*' ) === false ) 
    {
        $first = strstr($variable, '^', true);
        $last = strstr($variable, '^');
    }
    
    echo $first;
    echo '<br/>';
    echo $last;
    

    这一个正在处理每一个案件。

        3
  •  1
  •   MIK    7 年前

    谢谢你的回答。
    我已经根据你们提供的参考资料想出了我的解决方案。
    我正在发布它,将来可能会帮助任何人。。

    <?php
    
    $variable = 'Chloride TDS';
    if (preg_match('/[\*^]/', $variable))
    {
    if ( strpos( $variable, '*' ) < strpos( $variable, '^' )   || strpos( $variable, '^' ) === false ) 
    {
        $first = strstr($variable, '*', true);
    
        $last = strstr($variable, '*');
    }
    
    if ( strpos( $variable, '^' ) < strpos( $variable, '*' ) && strpos($variable,'^') != '' || strpos( $variable, '*' ) === false ) 
    {
        $first = strstr($variable, '^', true);
        $last = strstr($variable, '^');
    }
    echo $first;
    echo '<br/>';
    echo $last;
    }else{
        echo $variable;
    }
    
    
    
    ?>
    
        4
  •  0
  •   Roham Shojaei    7 年前

    $words = explode('*', $string, 2);
    

    $words = preg_split("/[\*,\^]+/", $str, -1, PREG_SPLIT_NO_EMPTY);