Tha*_*yen 7 php regex pcre preg-match
我的字符串是:"reply-234-private",我想在"回复"之后和"-private"之前得到号码,它是"234".我尝试使用以下代码,但它返回一个空结果:
$string = 'reply-234-private';
$display = preg_replace('/reply-(.*?)-private/','',$string);
echo $display;
Run Code Online (Sandbox Code Playgroud)
sac*_*een 21
你可以爆炸它:
<?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
Run Code Online (Sandbox Code Playgroud)
或者,使用preg_match
<?php
$string = 'reply-234-private';
if (preg_match('/reply-(.*?)-private/', $string, $display) === 1) {
echo $display[1];
}
Run Code Online (Sandbox Code Playgroud)
这样的事情:
$myString = 'reply-234-private';
$myStringPartsArray = explode("-", $myString);
$answer = $myStringPartsArray[1];
Run Code Online (Sandbox Code Playgroud)
小智 7
本文向您展示如何获取两个标记或两个字符串之间的所有字符串.
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);
}
?>
Run Code Online (Sandbox Code Playgroud)
万一你的问题,你可以试试这个:
$string1='reply-234-private';
echo GetStringBetween ($string1, "-", "-")
Run Code Online (Sandbox Code Playgroud)
或者我们可以使用任何'标识符字符串'来获取标识符字符串之间的字符串.例如:
echo GetStringBetween ($string1, "reply-", "-private")
Run Code Online (Sandbox Code Playgroud)