php 提取字符串PHP中两个字符之间的子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14891743/
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 08:08:10  来源:igfitidea点击:

Extract a substring between two characters in a string PHP

phpsqlsubstr

提问by Sebastian

Is there a PHP function that can extract a phrase between 2 different characters in a string? Something like substr();

是否有一个 PHP 函数可以提取字符串中 2 个不同字符之间的短语?类似的东西substr()

Example:

例子:

$String = "[modid=256]";

$First = "=";
$Second = "]";

$id = substr($string, $First, $Second);

Thus $idwould be 256

这样$id就会256

Any help would be appreciated :)

任何帮助,将不胜感激 :)

回答by Yogesh Suthar

use this code

使用此代码

$input = "[modid=256]";
preg_match('~=(.*?)]~', $input, $output);
echo $output[1]; // 256

working example http://codepad.viper-7.com/0eD2ns

工作示例http://codepad.viper-7.com/0eD2ns

回答by MaxEcho

Use:

用:

<?php

$str = "[modid=256]";
$from = "=";
$to = "]";

echo getStringBetween($str,$from,$to);

function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}

?>

回答by cartina

$String = "[modid=256]";

$First = "=";
$Second = "]";

$Firstpos=strpos($String, $First);
$Secondpos=strpos($String, $Second);

$id = substr($String , $Firstpos, $Secondpos);

回答by tasmaniski

If you don't want to use reqular expresion, use strstr, trimand strrevfunctions:

如果您不想使用 reqular 表达式,请使用strstrtrimstrrev函数:

// Require PHP 5.3 and higher
$id = trim(strstr(strstr($String, '='), ']', true), '=]');

// Any PHP version
$id = trim(strrev(strstr(strrev((strstr($String, '='))), ']')), '=]');

回答by Prasanth Bendra

$str = "[modid=256]";
preg_match('/\[modid=(?P<modId>\d+)\]/', $str, $matches);

echo $matches['modId'];

回答by Gerald Schneider

Regular Expression is your friend.

正则表达式是你的朋友。

preg_match("/=(\d+)\]/", $String, $matches);
var_dump($matches);

This will match any number, for other values you will have to adapt it.

这将匹配任何数字,对于其他值,您必须对其进行调整。

回答by Bgi

You can use a regular expression:

您可以使用正则表达式:

<?php
$string = "[modid=256][modid=345]";
preg_match_all("/\[modid=([0-9]+)\]/", $string, $matches);

$modids = $matches[1];

foreach( $modids as $modid )
  echo "$modid\n";

http://eval.in/9913

http://eval.in/9913

回答by tillinberlin

(moved from comment because formating is easier here)

(从评论中移出,因为这里的格式更容易)

might be a lazy approach, but in such cases i usually would first explode my string like this:

可能是一种懒惰的方法,但在这种情况下,我通常会首先像这样分解我的字符串:

$string_array = explode("=",$String); 

and in a second step i would get rid of that "]" maybe through rtrim:

在第二步中,我可能会通过 rtrim 摆脱那个“]”:

$id = rtrim($string_array[1], "]");

...but this will onlywork if the structure is always exactly the same...

...但这只有在结构始终完全相同的情况下才有效......

-cheers-

-干杯-

ps: shouldn't it actually be $String = "[modid=256]";?

ps:它实际上不应该是$String = "[modid=256]"; ?

回答by Reshil

Try Regular Expression

试试正则表达式

$String =" [modid=256]";
$result=preg_match_all('/(?<=(=))(\d+)/',$String,$matches);
print_r($matches[0]);

Output

输出

Array ( [0] => 256 )

数组 ( [0] => 256 )

DEMO

演示

ExplanationHere its used the Positive Look behind (?<=)in regular expression eg (?<=foo)bar matches bar when preceded by foo, here (?<=(=))(\d+) we match the (\d+) just after the '=' sign. \d Matches any digit character (0-9). + Matches 1 or more of the preceeding token

解释这里它使用了正则表达式中 (?<=) 后面的 Positive Look,例如 (?<=foo)bar 在 foo 前面匹配 bar,这里 (?<=(=))(\d+) 我们匹配 (\d+ ) 就在 '=' 符号之后。\d 匹配任何数字字符 (0-9)。+ 匹配 1 个或多个前面的标记

回答by pacmora

You can use a regular expression or you can try this:

您可以使用正则表达式,也可以试试这个:

$String = "[modid=256]";

$First = "=";
$Second = "]";
$posFirst = strpos($String, $First); //Only return first match
$posSecond = strpos($String, $Second); //Only return first match

if($posFirst !== false && $posSecond !== false && $posFirst < $posSecond){
    $id = substr($string, $First, $Second);
}else{
//Not match $First or $Second
}

You should read about substr. The best way for this is a regular expression.

你应该阅读 substr。最好的方法是使用正则表达式。