php中怎么获取一个字符串中空格前面的所有字符,PHP去除字符串中的所有空格及在每个字符前后加上‘%’...

function str_split_unicode($str, $l = 0) {

if ($l > 0) {

$ret = array();

$len = mb_strlen($str, "UTF-8");

for ($i = 0; $i < $len; $i += $l) {

$ret[] = mb_substr($str, $i, $l, "UTF-8");

}

return $ret;

}

return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);

}

$str = ' z 十 三';

echo strlen($str),'--strlen','
';

echo mb_strlen($str, 'utf-8'),'--mb_strlen','
';

$arrstr = str_split($str);

$arrstr = str_split_unicode($str);//符合要求

$temp='';

foreach ($arrstr as $val){

$temp.= trim($val);

}

echo $temp, '
';//符合要求,去除空格后的字符串

$arrstr = str_split_unicode($temp);//符合要求

$temp='%';

foreach ($arrstr as $val){

$temp.=$val.'%';

}

echo $temp,'
';//符合要求,加上‘%’后的字符串

echo mb_strlen($temp),'
';

echo mb_strlen($temp, 'utf-8');//符合要求

下面用java代码来实现/**

*

*/

package cn.com.songjy.demo;

/**

* @author songjianyong

*

*/

public class LikeSqlConditionDemo {

public static void main(String[] args) {

System.out.println(getLikeSqlCondition(" aa a d "));//输出结果是:%a%a%a%d%

}

public static String getLikeSqlCondition(String condition){

if(condition==null || condition.trim().length()==0)

return null;

condition = trim(condition);//去除空格

String[] str = condition.split("");

String temp = "";

for (String string : str) {

temp+=string+"%";

}

return temp;

}

public static String trim(String str){

String temp = "";

for(int i=0; i

temp = (new StringBuilder()).append(temp).append(str.substring(i, i+1).trim()).toString();

}

return temp;

}

}

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值