Home  >  Article  >  Backend Development  >  PHP去除字符串中的所有空格及在每个字符前后加下‘%’

PHP去除字符串中的所有空格及在每个字符前后加下‘%’

WBOY
WBOYOriginal
2016-06-13 13:20:18869browse

PHP去除字符串中的所有空格及在每个字符前后加上‘%’

function str_split_unicode($str, $l = 0) {
     if ($l > 0) {
         $ret = array();
         $len = mb_strlen($str, "UTF-8");
         for ($i = 0; $i ';
echo mb_strlen($str, 'utf-8'),'--mb_strlen','<br>';

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

$temp='';
foreach ($arrstr as $val){
	$temp.= trim($val);
}
echo $temp, '<br>';//符合要求,去除空格后的字符串
$arrstr = str_split_unicode($temp);//符合要求
$temp='%';
foreach ($arrstr as $val){
	$temp.=$val.'%';
}
echo $temp,'<br>';//符合要求,加上‘%’后的字符串
echo mb_strlen($temp),'<br>';
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<str.length i temp="(new" stringbuilder return><br><br><br>在进行like语句查询时候用的到
 <div class="clear">
                 
              
              
        
            </div></str.length>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn