【问题标题】:Get string between two strings获取两个字符串之间的字符串
【发布时间】:2012-12-28 02:18:10
【问题描述】:

我的字符串是:“reply-234-private”,我想得到“reply-”之后和“-private”之前的数字,它是“234”。我尝试使用以下代码,但它返回一个空结果:

$string = 'reply-234-private';
$display = preg_replace('/reply-(.*?)-private/','',$string);
echo $display;

【问题讨论】:

  • 你在使用 preg_replace(),你不想要 preg_match() 吗??

标签: php regex preg-match pcre


【解决方案1】:

你可以explode它:

<?php
$string = 'reply-234-private';
$display = explode('-', $string);

var_dump($display);
// prints array(3) { [0]=> string(5) "reply" [1]=> string(3) "234" [2]=> string(7) "private" }

echo $display[1];
// prints 234

或者,使用preg_match

<?php
$string = 'reply-234-private';
if (preg_match('/reply-(.*?)-private/', $string, $display) === 1) {
    echo $display[1];
}

【讨论】:

    【解决方案2】:

    看看explode() function

    类似这样的:

    $myString = 'reply-234-private';
    
    $myStringPartsArray = explode("-", $myString);
    
    $answer = $myStringPartsArray[1];
    

    【讨论】:

      【解决方案3】:

      本文向您展示,如何获取两个标签或两个字符串之间的所有字符串。

      http://okeschool.com/articles/312/string/how-to-get-of-everything-string-between-two-tag-or-two-strings

      <?php
       // Create the Function to get the string
       function GetStringBetween ($string, $start, $finish) {
          $string = " ".$string;
          $position = strpos($string, $start);
          if ($position == 0) return "";
          $position += strlen($start);
          $length = strpos($string, $finish, $position) - $position;
          return substr($string, $position, $length);
      }
      ?>
      

      如果你有问题,你可以试试这个:

      $string1='reply-234-private';
      echo GetStringBetween ($string1, "-", "-")
      

      或者我们可以使用任何“标识符字符串”来获取标识符字符串之间的字符串。例如:

      echo GetStringBetween ($string1, "reply-", "-private")
      

      【讨论】:

        【解决方案4】:

        使用 php 内置的正则表达式支持函数 preg_match_all

        这会有所帮助,假设您想要在以下示例中 @@ 之间的字符串(键)数组,其中 '/' 不介于两者之间,您可以使用不同的 start构建新示例> 结束变量

        function getInbetweenStrings($start, $end, $str){
            $matches = array();
            $regex = "/$start([a-zA-Z0-9_]*)$end/";
            preg_match_all($regex, $str, $matches);
            return $matches[1];
        }
        
        $str = "C://@@ad_custom_attr1@@/@@upn@@/@@samaccountname@@";
        $str_arr = getInbetweenStrings('@@', '@@', $str);
        
        print_r($str_arr);
        

        【讨论】:

          【解决方案5】:
          $myString = 'reply-234-private';
          echo str_replace('-','',filter_var($myString,FILTER_SANITIZE_NUMBER_INT));
          

          这应该可以完成工作。

          【讨论】:

            【解决方案6】:

            如果你想用js做,试试这个功能-

            function getStringBetween(str , fromStr , toStr){
              var fromStrIndex = str.indexOf(fromStr) == -1 ? 0 : str.indexOf(fromStr) + fromStr.length;
              var toStrIndex = str.slice(fromStrIndex).indexOf(toStr) == -1 ? str.length-1 : str.slice(fromStrIndex).indexOf(toStr) + fromStrIndex;
              var strBtween = str.substring(fromStrIndex,toStrIndex);
              return strBtween;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-09-12
              • 1970-01-01
              • 2018-11-16
              • 1970-01-01
              相关资源
              最近更新 更多