假设我有以下用户 DN
我需要从下面的字符串中获取唯一的 DC
字符串和值。
示例 1:
CN=xxx,OU=xxx xxx,OU=aaa bbb,DC=ABC,DC=com
结果 1:
DC=ABC,DC=com
示例 2:创建者 Sam J Levy
OU=Departments,DC=college,DC=school,DC=edu
结果 2:
DC=college,DC=school,DC=edu
有没有办法像上面的results那样得到整个DC字符串? Sample Demo
最佳答案
PHP
您可以像这样将 substr
与 strpos
结合起来:
<?php
$string = 'CN=xxx,OU=xxx xxx,OU=aaa bbb,DC=ABC,DC=com';
echo substr($string, strpos($string, 'DC='));
?>
输出:
DC=ABC,DC=com
示例 2:
<?php
$string = 'OU=Departments,DC=college,DC=school,DC=edu';
echo substr($string, strpos($string, 'DC='));
?>
输出:
DC=college,DC=school,DC=edu
JavaScript
您可以使用这个 strpos使用 substring
和 length
<script>
function strpos(haystack, needle, offset) {
var i = (haystack + '')
.indexOf(needle, (offset || 0));
return i === -1 ? false : i;
}
var string = 'CN=xxx,OU=xxx xxx,OU=aaa bbb,DC=ABC,DC=com';
var pos = strpos(string, 'DC=');
var info = string.substring(pos, string.length);
alert(info);
</script>
示例 2:
<script>
function strpos(haystack, needle, offset) {
var i = (haystack + '')
.indexOf(needle, (offset || 0));
return i === -1 ? false : i;
}
var string = 'OU=Departments,DC=college,DC=school,DC=edu';
var pos = strpos(string, 'DC=');
var info = string.substring(pos, string.length);
alert(info);
</script>
阅读更多:
http://php.net/manual/en/function.substr.php
关于javascript - 如何在 PHP 中获取特定的字符串单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482757/