如何替换PHP字符串中的表意空格?

如何替换PHP字符串中的表意空格?,php,Php,表意空间是,它是一个CJK标点符号。它看起来像一个普通的空格,但实际上它在屏幕上占据2个位置,而不像汉字那样占据1个位置 我试着用str_replace、$mystring来摆脱它们,但当然不行,因为我在这里输入的空间是ASCII空间。我也尝试过用汉字输入法手动输入表意空间,但看起来这样我也会去掉部分其他字符的代码,它会返回乱码 那么我怎样才能去掉这些空格呢?我可以通过从链接到的信息页面复制符号来替换字符。您可能希望为表意空间创建一个CONST别名,以帮助使和查找/替换编码更加清晰 // con

表意空间是,它是一个CJK标点符号。它看起来像一个普通的空格,但实际上它在屏幕上占据2个位置,而不像汉字那样占据1个位置

我试着用str_replace、$mystring来摆脱它们,但当然不行,因为我在这里输入的空间是ASCII空间。我也尝试过用汉字输入法手动输入表意空间,但看起来这样我也会去掉部分其他字符的代码,它会返回乱码


那么我怎样才能去掉这些空格呢?

我可以通过从链接到的信息页面复制符号来替换字符。您可能希望为表意空间创建一个CONST别名,以帮助使和查找/替换编码更加清晰

// contains ideographic space between words
$start = 'before after';                    

// contains ideographic space in needle parameter
$test1 = str_replace(' ', '_', $start);     

// contains ideographic space
define('ID_SPACE', ' ');                    
$test2 = str_replace(ID_SPACE, '&', $start);

// contains normal space in needle parameter
$test3 = str_replace(' ','_',$start);       

// make sure we are using utf8 for this test
header('Content-Type: text/html; charset=utf-8');

echo $start.'<br/>';
echo $test1.'<br/>';
echo $test2.'<br/>';
echo $test3;
根据问题进行编辑

当您看不到该字符时,该字符将显示在显示的框中,只需单击并拖动以像选择任何其他文本一样进行选择,然后您可以根据需要粘贴该字符。你也可以从我的答案中复制包含空格的代码。如果您看到类似于ã€的内容,则需要将字符集设置为utf-8


您可以直接从转义的数值转换对象。多年来,我一直有以下功能。不是我写的,恐怕我不记得在哪里找到的。这有点像黑客,但我认为是非常有用的

<?php

function code2utf($num) {
  if($num<128)return chr($num);
  if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);
  if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
  if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128);
  return '';
}

print "a" . code2utf(0x3000) . "b" . code2utf(0x1f44d) . "\n";
当我运行这个时,我看到:

$ php -f utftest
a bA method that also worked for me, raw encoding it into HTML Entities & 
str_replace
back to a normal whitespace.

//The space we're looking out for
$ideoSpace      = "%26%23x3000%3B";
$space          = "%20";

//Search string (Notice the wider space)
$searchstr = "Please find me a Oil Filter";

//Begin conversion
$searchstr = rawurldecode( str_replace( $ideoSpace, $space, rawurlencode( $searchstr )));

//echos "Please find me a Oil Filter"
$php-f utftest
A. b一种对我也有效的方法,将其原始编码为HTML实体&str_替换回正常的空白


也许不是最优雅的解决方案。然而不幸的是,搜索对我们不起作用,因为内爆无法为我们的日本客户分割字符串

@AlvaroFlañoLarrondo。。对不起,没有。试试:php-r'print-trimhtml\u-entity\u-decode&x3000;a\n、 “。。并清楚地列出了它认为是空白的字符,0x3000不是其中之一。
$ php -f utftest
a bA method that also worked for me, raw encoding it into HTML Entities & 
str_replace
back to a normal whitespace.

//The space we're looking out for
$ideoSpace      = "%26%23x3000%3B";
$space          = "%20";

//Search string (Notice the wider space)
$searchstr = "Please find me a Oil Filter";

//Begin conversion
$searchstr = rawurldecode( str_replace( $ideoSpace, $space, rawurlencode( $searchstr )));

//echos "Please find me a Oil Filter"