Is there a php function that can extract a phrase between 2 different characters in a string? Something like substr();
是否有一个php函数可以在字符串中的两个不同字符之间提取短语?类似substr();
Example:
例子:
$String = [modid=256];
$First = "=";
$Second = "]";
$id = substr($string, $First, $Second);
Thus $id
would be 256
因此$id为256
Any help would be appreciated :)
如有任何帮助,我们将不胜感激。
10 个解决方案
#1
49
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工作示例
#2
20
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));
}
?>
#3
4
$String = "[modid=256]";
$First = "=";
$Second = "]";
$Firstpos=strpos($String, $First);
$Secondpos=strpos($String, $Second);
$id = substr($String , $Firstpos, $Secondpos);
#4
3
If you don't want to use reqular expresion, use strstr, trim and strrev functions:
如果您不想使用请求程序,请使用strstr、trim和strrev函数:
// Require PHP 5.3 and higher
$id = trim(strstr(strstr($String, '='), ']', true), '=]');
// Any PHP version
$id = trim(strrev(strstr(strrev((strstr($String, '='))), ']')), '=]');
#5
1
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.
这将匹配任何数字,对于其他值,您将不得不调整它。
#6
1
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
#7
1
$str = "[modid=256]";
preg_match('/\[modid=(?P<modId>\d+)\]/', $str, $matches);
echo $matches['modId'];
#8
0
(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 only work if the structure is always exactly the same...
…但这只有在结构总是完全相同的情况下才行得通……
-cheers-
干杯,
ps: shouldn't it actually be $String = "[modid=256]"; ?
是不是应该是$String = "[modid=256]" ?吗?
#9
0
Try Regular Expression
试的正则表达式
$String =" [modid=256]";
$result=preg_match_all('/(?<=(=))(\d+)/',$String,$matches);
print_r($matches[0]);
Output
输出
Array ( [0] => 256 )
数组([0]=> 256)
演示
Explanation Here 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
这里的解释是,它在正则表达式eg (?<=foo)bar中使用了带正值的Look behind (?\d匹配任何数字字符(0-9)。+匹配一个或多个前标记
#10
-1
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。最好的方法是使用正则表达式。
#1
49
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工作示例
#2
20
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));
}
?>
#3
4
$String = "[modid=256]";
$First = "=";
$Second = "]";
$Firstpos=strpos($String, $First);
$Secondpos=strpos($String, $Second);
$id = substr($String , $Firstpos, $Secondpos);
#4
3
If you don't want to use reqular expresion, use strstr, trim and strrev functions:
如果您不想使用请求程序,请使用strstr、trim和strrev函数:
// Require PHP 5.3 and higher
$id = trim(strstr(strstr($String, '='), ']', true), '=]');
// Any PHP version
$id = trim(strrev(strstr(strrev((strstr($String, '='))), ']')), '=]');
#5
1
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.
这将匹配任何数字,对于其他值,您将不得不调整它。
#6
1
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
#7
1
$str = "[modid=256]";
preg_match('/\[modid=(?P<modId>\d+)\]/', $str, $matches);
echo $matches['modId'];
#8
0
(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 only work if the structure is always exactly the same...
…但这只有在结构总是完全相同的情况下才行得通……
-cheers-
干杯,
ps: shouldn't it actually be $String = "[modid=256]"; ?
是不是应该是$String = "[modid=256]" ?吗?
#9
0
Try Regular Expression
试的正则表达式
$String =" [modid=256]";
$result=preg_match_all('/(?<=(=))(\d+)/',$String,$matches);
print_r($matches[0]);
Output
输出
Array ( [0] => 256 )
数组([0]=> 256)
演示
Explanation Here 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
这里的解释是,它在正则表达式eg (?<=foo)bar中使用了带正值的Look behind (?\d匹配任何数字字符(0-9)。+匹配一个或多个前标记
#10
-1
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。最好的方法是使用正则表达式。