PHP :如何 select 字符串的特定部分
PHP : How to select specific parts of a string
我想知道...我有两个字符串 :
"CN=CMPPDepartemental_Direction,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan",
"CN=CMPPDepartemental_Secretariat,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan"
有没有办法在 php 中只 select 这些字符串的第一部分?我只想 select CMPPDepartemental_Direction 和 CMPPDepartemental_Secretariat.
我曾想尝试使用 substr() 或 trim() 但没有成功。
您应该使用 preg_match
和正则表达式 CN=(\w+_\w+)
来提取需要的部分:
$strs = [
"CN=CMPPDepartemental_Direction,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan",
"CN=CMPPDepartemental_Secretariat,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan"
];
foreach ($strs as $str) {
$matches = null;
preg_match('/CN=(\w+_\w+)/', $str, $matches);
echo $matches[1];
}
如果字符串总是具有相同的结构,我建议使用自定义函数 find_by_keyword
- 这样您也可以搜索其他关键字。
function find_by_keyword( $string, $keyword ) {
$array = explode(",",$string);
$found = [];
// Loop through each item and check for a match.
foreach ( $array as $string ) {
// If found somewhere inside the string, add.
if ( strpos( $string, $keyword ) !== false ) {
$found[] = substr($string, strlen($keyword));
}
}
return $found;
}
var_dump(find_by_keyword($str2, "CN="));
// array(1) {
[0]=>
string(27) "CMPPDepartemental_Direction"
}
var_dump(find_by_keyword($str2, "OU="));
//array(4) {
[0]=>
string(25) "1 - Groupes de sécurité"
[1]=>
string(4) "CMPP"
[2]=>
string(4) "Pole"
[3]=>
string(12) "Utilisateurs"
}
例子here.
我想知道...我有两个字符串 :
"CN=CMPPDepartemental_Direction,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan",
"CN=CMPPDepartemental_Secretariat,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan"
有没有办法在 php 中只 select 这些字符串的第一部分?我只想 select CMPPDepartemental_Direction 和 CMPPDepartemental_Secretariat.
我曾想尝试使用 substr() 或 trim() 但没有成功。
您应该使用 preg_match
和正则表达式 CN=(\w+_\w+)
来提取需要的部分:
$strs = [
"CN=CMPPDepartemental_Direction,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan",
"CN=CMPPDepartemental_Secretariat,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan"
];
foreach ($strs as $str) {
$matches = null;
preg_match('/CN=(\w+_\w+)/', $str, $matches);
echo $matches[1];
}
如果字符串总是具有相同的结构,我建议使用自定义函数 find_by_keyword
- 这样您也可以搜索其他关键字。
function find_by_keyword( $string, $keyword ) {
$array = explode(",",$string);
$found = [];
// Loop through each item and check for a match.
foreach ( $array as $string ) {
// If found somewhere inside the string, add.
if ( strpos( $string, $keyword ) !== false ) {
$found[] = substr($string, strlen($keyword));
}
}
return $found;
}
var_dump(find_by_keyword($str2, "CN="));
// array(1) {
[0]=>
string(27) "CMPPDepartemental_Direction"
}
var_dump(find_by_keyword($str2, "OU="));
//array(4) {
[0]=>
string(25) "1 - Groupes de sécurité"
[1]=>
string(4) "CMPP"
[2]=>
string(4) "Pole"
[3]=>
string(12) "Utilisateurs"
}
例子here.