PHP 字符串问题
发布网友
发布时间:2022-04-18 10:21
我来回答
共3个回答
热心网友
时间:2022-04-18 11:51
要重复还是没重复呢?
这个是输出所有的小写字母,不管是不是重复的,比如你输入的字符串为aa,则输出aa
<?php
function getLower($str){
$len = strlen($str);
$newstr = '';
$lower = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
for($i=0;$i<$len;$i++){
if(in_array($str[$i],$lower)){
$newstr .= $str[$i];
}
}
return $newstr;
}
$str = "youWant";//根据需要定义你的字符串
$str = getLower($str);//调用函数后得到的小写字母的字符串
?>
这个是输出不重复的
<?php
function getLower($str){
$len = strlen($str);
$newstr = array();
$lower = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
for($i=0;$i<$len;$i++){
if(in_array($str[$i],$lower)){
if(!in_array($str[$i],$newstr)){
$newstr[] = $str[$i];
}
}
}
return implode("",$newstr);
}
echo getLower("aaBBacde");//将输出acde,重复的a不输出
?>
你根据需要选你需要的
热心网友
时间:2022-04-18 13:09
<?php
$str = 'abcdefghijklmnopqrstuvwxyz';
function get_the_char($str) {
$ary = array();
for($i = 0 ; $i < 26 ; ++$i) {
$ary[$i] = chr(97+$i);
}
for($i = 0 , $j = strlen($str); $i < $j ; $i++) {
$k = ord(substr($str , $i ,1)) - 97;
if(isset($ary[$k])){
unset($ary[$k]);
}
}
return $ary;
}
echo join(get_the_char($str) , '<li>');
?>
热心网友
时间:2022-04-18 14:43
下面是我刚刚写的程序,你可以运行下看看,结果~~~~~注意,那些中文全部都是注释,帮助你理解的,考过去后,你调整下格式就可以运行了;
<?php
$arr=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
//定义一个字符串,也就是你要输入的英文字符串;
$str="abdcaf";
if (preg_match("([a-z]+)",$str)){//判断所给的字符串是不是小写字母a-z/组成;
//定义一个数组用于保存对应字符在a-z这数组当中对应的下标;
$xb=array();
//将字符串拆分成数组;
$re=str_split($str);
//将拆分后的数组进行循环,取出其中的一项用于与a-z这个数组进行比较;
for($k=0;$k<count($re);$k++){
for($i=0;$i<count($arr);$i++){//取出a-z这个数组中的一项与拆分后的数组项进行比较;
if ($re[$k]==$arr[$i]){//如果相等,则将对应字母的下标存入到xb这个数组当中去;
array_push($xb,$i);
}
continue;//不相等则进行下一次循环;
}
}
//这个是个关键步骤,因为我们要把两个数组的下标进行比较,所以要用foreach循环;
foreach ($arr as $key=>$value){
$xbs=0;//定义一个标示,如果找到该下标则将1赋值给该标示;
for ($d=0;$d<count($xb);$d++){
if ($key==$d){
$xbs=1;
break;
}else{
continue;
}
}
//循环结束,我们判决标示是不是0就可以啦;
if ($xbs==0){
echo $value."<br>";
}
}
}
?>