[英]Extract a substring between two characters in a string PHP
Is there a PHP function that can extract a phrase between 2 different characters in a string?是否有一个 PHP 函数可以提取字符串中 2 个不同字符之间的短语? Something like
substr()
;像
substr()
类的东西;
Example:例子:
$String = "[modid=256]";
$First = "=";
$Second = "]";
$id = substr($string, $First, $Second);
Thus $id
would be 256
因此
$id
将是256
Any help would be appreciated :)任何帮助,将不胜感激 :)
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
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));
}
?>
$String = "[modid=256]";
$First = "=";
$Second = "]";
$Firstpos=strpos($String, $First);
$Secondpos=strpos($String, $Second);
$id = substr($String , $Firstpos, $Secondpos);
If you don't want to use reqular expresion, use strstr , trim and strrev functions:如果您不想使用 reqular 表达式,请使用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, '='))), ']')), '=]');
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.这将匹配任何数字,对于其他值,您必须对其进行调整。
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";
$str = "[modid=256]";
preg_match('/\[modid=(?P<modId>\d+)\]/', $str, $matches);
echo $matches['modId'];
(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]"; ps:它实际上不应该是$String = "[modid=256]"; ?
?
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.解释这里它使用了正则表达式中 (?<=) 后面的 Positive Look,例如 (?<=foo)bar 在 foo 前面匹配 bar,这里 (?<=(=))(\\d+) 我们匹配 (\\d+ ) 就在 '=' 符号之后。 \\d Matches any digit character (0-9).
\\d 匹配任何数字字符 (0-9)。 + Matches 1 or more of the preceeding token
+ 匹配 1 个或多个前面的标记
I think this is the way to get your string:我认为这是获取字符串的方法:
<?php
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = '.layout {
color: {{ base_color }}
}
li {
color: {{ sub_color }}
}
.text {
color: {{ txt_color }}
}
.btn {
color: {{ btn_color }}
}
.more_text{
color:{{more_color}}
}';
$parsed = get_string_between($fullstring, '{{', '}}');
echo $parsed;
?>
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.你应该阅读 substr。 The best way for this is a regular expression.
最好的方法是使用正则表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.