PHP:如何选择字符串的特定部分

发布于 2025-01-26 05:14:09 字数 545 浏览 3 评论 0原文

我想知道...我有两个字符串:

"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中是否有一种方法可以选择这些字符串的第一部分?我只想选择 cmppdepartemental_direction cmppdepartemental_secretariat

我曾经想过尝试使用substr()或trim(),但没有成功。

I was wondering... I have two strings :

"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"

Is there a way in php to select only the first part of these strings ? I would like to just select CMPPDepartemental_Direction and CMPPDepartemental_Secretariat.

I had thought of trying with substr() or trim() but without success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2025-02-02 05:14:09

您应该将Preg_match与Regex 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];
}

You should use preg_match with regex CN=(\w+_\w+) to extract needed parts:

$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];
}
黑寡妇 2025-02-02 05:14:09

如果字符串始终具有相同的结构,我建议使用自定义函数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"
}

审查在这里

If the strings always have the same structure, I recommend using a custom function find_by_keyword - so you can search for other keywords too.

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"
}

Examle here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文