我想从字符串中提取变量的第一个单词.例如,请输入以下内容:
<?php $myvalue = 'Test me more'; ?>
Run Code Online (Sandbox Code Playgroud)
结果输出应该是Test输入的第一个字.我怎样才能做到这一点?
鉴于某些多字节字符集,我是否正确假设以下内容不符合预期目的?
$string = str_replace('"', '\\"', $string);
Run Code Online (Sandbox Code Playgroud)
特别是,如果输入的字符集可能具有像0xbf5c这样的有效字符,那么攻击者可以注入0xbf22来获取0xbf5c22,留下一个有效字符后跟一个不带引号的双引号(").
有没有一种简单的方法来缓解这个问题,或者我是否首先误解了这个问题?
(在我的例子中,字符串进入HTML输入标签的value属性:echo'input type ="text"value ="'.$ string.'">';)
编辑:就此而言,像preg_quote()这样的函数呢?它没有charset论据,因此在这种情况下似乎完全没用.如果你没有选择将字符集限制为UTF-8(是的,这很好),你好像很残疾.在这种情况下可以使用哪些替换和引用功能?
PHP str_replace()仅适用于ANSI字符串,因此可以破坏UTF-8字符串.但是,如果只有有效的UTF-8字符串作为参数,那么它是二进制安全的吗?
编辑:我不是在寻找替代函数,我只想知道这个假设是否正确.
我正在尝试为PHP中的名称编写一个合理宽松的验证器,我的第一次尝试包含以下模式:
// unicode letters, apostrophe, hyphen, space
$namePattern = "/^([\\p{L}'\\- ])+$/";
Run Code Online (Sandbox Code Playgroud)
这最终传递给了一个电话preg_match().据我所知,这适用于你的vanilla ASCII字母表,但似乎惹上像Ă或张这样的尖锐字符.
这个模式本身有什么问题吗?也许我期待\p{L}比我想的更多的工作?
或者它与传入输入的方式有关?我不确定它是否相关,但我确实确保在表单页面上指定UTF8编码.
我有这个文字:
$text = "Ba?ka, küskün otomobil kaçt? buraya küskün otomobil neden kaçt?
kaçt? buraya, oraya KISMEN @here #there J.J.Johanson hep.
Danny:Where is mom? I don't know! Café est weiß for 2 €uros.
My 2nd nickname is mike18.";
Run Code Online (Sandbox Code Playgroud)
最近我用这个.
$a1= array_count_values(str_word_count($text, 1, 'ÇçÖö???I???Üü@#é߀1234567890'));
arsort($a1);
Run Code Online (Sandbox Code Playgroud)
您可以查看这个小提琴:http:
//ideone.com/oVUGYa
但是这个解决方案并不能解决所有UTF8问题.我不能将整个UTF8集写入str_word_count作为参数.
所以我创造了这个:
$wordsArray = explode(" ",$text);
foreach ($wordsArray as $k => $w) {
$wordsArray[$k] = str_replace(array(",","."),"",$w);
}
$wordsArray2 = array_count_values($wordsArray);
arsort($wordsArray2);
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的:
Array (
[kaçt?] => 3
[küskün] => 2
[buraya] …Run Code Online (Sandbox Code Playgroud) NO-BREAK SPACE和许多其他UTF-8符号 需要2个字节才能表示 ; 因此,在UTF8字符串的假设上下文中,非ASCII(> 127)的隔离(非xC2前面)字节是一个不可识别的字符......好吧,它只是一个布局问题(!),但它破坏了整个字符串?
如何避免这种"非预期的行为"?(它出现在某些功能中,而不是在其他功能中).
示例(仅生成非预期行为preg_match):
header("Content-Type: text/plain; charset=utf-8"); // same if text/html
//PHP Version 5.5.4-1+debphp.org~precise+1
//using a .php file enconded as UTF8.
$s = "THE UTF-8 NO-BREAK\xA0SPACE"; // a non-ASCII byte
preg_match_all('/[-\'\p{L}]+/u',$s,$m);
var_dump($m); // empty! (corrupted)
$m=str_word_count($s,1);
var_dump($m); // ok
$s = "THE UTF-8 NO-BREAK\xC2\xA0SPACE"; // utf8-encoded nbsp
preg_match_all('/[-\'\p{L}]+/u',$s,$m);
var_dump($m); // ok!
$m=str_word_count($s,1);
var_dump($m); // ok
Run Code Online (Sandbox Code Playgroud) 我做了下一个函数,可以从文本中返回特定数量的单词:
function brief_text($text, $num_words = 50) {
$words = str_word_count($text, 1);
$required_words = array_slice($words, 0, $num_words);
return implode(" ", $required_words);
}
Run Code Online (Sandbox Code Playgroud)
并且它在英语中也能很好地工作,但是当我尝试在阿拉伯语中使用它时,它会失败并且不会返回预期的单词。例如:
$text_en = "Cairo is the capital of Egypt and Paris is the capital of France";
echo brief_text($text_en, 10);
Run Code Online (Sandbox Code Playgroud)
将Cairo is the capital of Egypt and Paris is the在
$text_ar = "??????? ?? ????? ??? ?????? ?? ????? ?????";
echo brief_text($text_ar, 10);
Run Code Online (Sandbox Code Playgroud)
将输出? ? ? ? ? ? ? ? ? ?。
我知道问题出在str_word_count函数上,但我不知道如何解决。 …