在php中搜索字符串,搜索字段包含空格 [英] Searching string in php, search field contains white space
问题描述
这是我的代码
<?php $a = strtolower($criteria);if (strpos($a, 'fits models from: 11/2000') !== false) {echo '适合 2000 年的模型,';?>
我的空白有问题.如果我只使用 'fits models from' 或者如果我使用 '11/2000',它会找到字符串,而不是两者的结合.我需要把它们组合起来,否则没用.
我不能做这样的事情:
<?php $a = strtolower($criteria);if (strpos($a, 'fits models from') !== false)如果 (strpos($a, '11/2000') !== false) {echo '适合 2000 年的模型,';?>
因为我想用它来将关键因素与文本(汽车模型)与其余部分分开并输出该信息.如果 $criteria 说 'fits model from 11/2000, but only fits model until 06/2002' 那么它将输出'fits model from 2000', 'fits model until 2000', 'fits model from 2002', 'fits model until2002'.
识别未知字符:
- 找到字符串编码(在您的情况下可能是 UTF-8)
- 用字符提取一个短子串
- 将其拆分为字节并以十六进制显示
- 查看编码表,找出用于编码它的字节是什么字符
带有字符串的示例:"a:\xe2\x80\x85b"
看起来像 a: b
但空间较小.
$str = "a:\xe2\x80\x85b";//我写了 \xe2\x80\x85 只是为了设置 $str 并显示一个工作代码,//但在这里我不知道这些字节的值是什么preg_match('~:(.*?)b~us', $str, $m);//: 和 b 之间的最短子串echo implode(' ', array_map(function ($b) { return dechex(ord($b)); }, str_split($m[1])));//e2 80 85
我得到3个字节e2
80
85
,然后我搜索它是否代表unicode table 并且我发现:U+2005 e2 80 85 四个每个 EM 空间
结论:未知字符是一个 FOUR-PER-EM SPACE
(Unicode 点:U+2005)并且需要将 3 个字节 e2 80 85
编码在UTF-8.所以我可以用双引号字符串写"\xe2\x80\x85"
.
This is my code
<div style="display:inline-block; ">
<?php $a = strtolower($criteria); if (strpos($a, 'fits models from: 11/2000') !== false) {
echo 'Fits models from 2000,'; } ?> </div>
I have a problem with the white space. It finds the string if I use only 'fits models from' or if I use '11/2000', but not the two combined. I need them combined or else it's usless.
EDIT: I can't do something like this:
<?php $a = strtolower($criteria);
if (strpos($a, 'fits models from') !== false)
if (strpos($a, '11/2000') !== false) {
echo 'Fits models from 2000,'; } ?> </div>
because I want to use it to seperate key factors from a text, which is the car model, from the rest and output that information. If $criteria says 'fits model from 11/2000, but only fits model until 06/2002' then it will output 'fits model from 2000', 'fits model until 2000', 'fits model from 2002', 'fits model until 2002'.
To identity an unknown character:
- find the string encoding (probably UTF-8 in your case)
- extract a short substring with the character
- split it into bytes and display them in hexadecimal
- looking at the encoding table, find what character it is with the byte(s) used to encode it
example with the string: "a:\xe2\x80\x85b"
that looks like a: b
but with a smaller space.
$str = "a:\xe2\x80\x85b"; // I wrote \xe2\x80\x85 only to set $str and to show a working code,
// but here I don't know what are the values of these bytes
preg_match('~:(.*?)b~us', $str, $m); // shortest substring between : and b
echo implode(' ', array_map(function ($b) { return dechex(ord($b)); }, str_split($m[1])));
// e2 80 85
I obtain the 3 bytes e2
80
85
, then I search if it represents one or several characters in the unicode table and I find: U+2005 e2 80 85 FOUR-PER-EM SPACE
Conclusion: the unknown character is a FOUR-PER-EM SPACE
(unicode point: U+2005) and needs the 3 bytes e2 80 85
to be encoded in UTF-8. So I can write it "\xe2\x80\x85"
in a double quoted string.
这篇关于在php中搜索字符串,搜索字段包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!