strpos()找出位置,substr()得到指定位置字符的前半段和后半段,然后根据你的需要拼接起来。

解决方案 »

  1.   

    <?php
    $str="123456789";
    $str1="qwertyuio";
    $pos = strpos($str, "5");
    $length = strlen($str1);
    $new_str1 = substr($str1, 0, $pos);
    $new_str2 = substr($str1, $pos, 1);
    $new_str3 = substr($str1, $pos+1, $length-$pos);
    $new_str2 = "<font color=\"#ff0000\">".$new_str2."</font>";
    $new_str = $new_str1.$new_str2.$new_str3;
    echo $new_str;
    ?>
      

  2.   

    $new_str1 = substr($str1, 0, $pos);
    $new_str2 = substr($str1, $pos, 1);
    $new_str3 = substr($str1, $pos+1, $length-$pos);
    请问这三段代码是什么作用,在这烦劳大家帮忙解答?谢谢!
      

  3.   

    根据$pos得到的数值 分割$str1    $new_str2为$str1中对应 $pos位置的字符  $new_str1 $new_str3为前后  不过好象有点麻烦哦 天热 懒的想...
      

  4.   

    其实我的最终目的是:
    $str="保定,石家庄,郑州,安康";
    $str1="17,22,67,89";
    通过查询,例如查询“石家庄”,输出的$str中的“石家庄”是红色显示的其余的还是黑色显示,$str1中对应的“22”也显示红色其余的也还是黑色显示。请问大家如何能实现。
      

  5.   

    use array  to define   $str && $str1
      

  6.   

    你的数据结构有问题。如果你要实现这样的功能,建议你用array的方式。通过key进行对应匹配,非常简单。如果你必须用string方式,那就用四楼的空格t给出的方法。substr的作用自己查手册。
      

  7.   

    用array的方式。通过key进行对应匹配,
    请指教,我没有用过这种方法,请详细说明,谢了
      

  8.   

    $str=array("17"=>"保定",("22"=>"石家庄",("67"=>"郑州",("89"=>"安康");then use foreach()
      

  9.   

    $str=array("17"=>"保定","22"=>"石家庄","67"=>"郑州","89"=>"安康");
    that's right
      

  10.   

    <?
    $subs="保定";
    $str=array("17"=>"保定","22"=>"石家庄","67"=>"郑州","89"=>"安康");
    foreach($str  as  $key  =>  $s) 
    {
     if($s===$sus){
    $s=$sus;
    $key=$key;
    }
    echo "<font color="FF0000">$s</font><br\ ><font color="FF0000">$key</font>"
    }
    ?>
      

  11.   

    <?
    $subs="保定";
    $str=array("17"=>"保定","22"=>"石家庄","67"=>"郑州","89"=>"安康");
    foreach($str  as  $key  =>  $s) 
    {
     if($s===$sus){
    $s=$sus;
    $key=$key;
    }
    echo "<font color="FF0000">$s</font><br\ ><font color="FF0000">$key</font>";
    }
    ?>
    not test waittttt to correct
      

  12.   

    首先是这个地方出错了
    echo "<font color=\"FF0000\">$s</font><br\ ><font color=\"FF0000\">$key</font>";
    其次,输出的字符串全是红色显示!!!
      

  13.   

    <?php
    $str="保定,石家庄,郑州,安康";
    $str1="17,22,67,89";$a = split(',|,', $str);
    $a1 = split(',|,', $str1);$s = '石家庄';
    $s1 = $a1[array_search($s, $a)];echo str_replace($s, "<font color=red>$s</font>", $str);
    echo str_replace($s1, "<font color=red>$s1</font>", $str1);?>
      

  14.   

    呵呵 xuzuning(唠叨)你一直在帮助我呀,谢谢了,我对正则表达式比较陌生,请问能在哪学呀?如何学习呀?看了也记不牢,请给点好的建议.
      

  15.   

    网上就很多教程,google一下即可
      

  16.   

    书啊,一般讲perl的都会讲到
    买点便宜墨水,打印出来看吧
      

  17.   

    xuzuning(唠叨) 
    <?php
    $str="保定,石家庄,郑州,安康";
    $str1="17,22,67,89";$a = split(',|,', $str);
    $a1 = split(',|,', $str1);$s = '石家庄';
    $s1 = $a1[array_search($s, $a)];echo str_replace($s, "<font color=red>$s</font>", $str);
    echo str_replace($s1, "<font color=red>$s1</font>", $str1);?>这段代码还是有一点小问题,你在$s中付值时,如果给出值是$str中不存在的值时,就回出现别的效果,请问如何修改??
      

  18.   

    那就先检测一下,比如$s1不存在:
    if(!$s1)
    {
         $s1="****";           //****你自己写啊
    }
    或者提示出错啊,看你怎么想了。