php 获取两个字符串之间的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14313075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:03:53  来源:igfitidea点击:

Get string between two strings

phpregexpreg-matchpcre

提问by Thanh Nguyen

My string is: "reply-234-private", i want to get the number after "reply-" and before "-private", it is "234". I have tried with following code but it returns an empty result:

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

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

回答by sachleen

You can just explodeit:

你可以爆炸它:

<?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

Or, use preg_match

或者,使用preg_match

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

回答by Kuncara Kurniawan

This article show you, How to get of everything string between two tags or two strings.

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

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

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);
}
?>

in case your question, you can try this :

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

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

or we can use any 'identifier string' for grab the string between the identifier string. for example:

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

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

回答by Chris Hymanson

Have a look at explode() function

看看explode()函数

something like this:

像这样:

$myString = 'reply-234-private';

$myStringPartsArray = explode("-", $myString);

$answer = $myStringPartsArray[1];

回答by Ravi Verma

Use php's inbuilt regex support functions preg_match_all

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

this would help, suppose you want the array of strings(keys) between @@ in following example, where '/' doesn't fall in-between, you can built new example with different startan endvariable

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

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);

回答by szikael

$myString = 'reply-234-private';
echo str_replace('-','',filter_var($myString,FILTER_SANITIZE_NUMBER_INT));

That should do the job.

那应该可以完成这项工作。

回答by user2951807

If you want to do it in js, try this function -

如果你想用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;
}