PHP explode()不适用于" -"特点 [英] PHP explode() not work with " - " character
问题描述
例如,我想对文本进行子字符串化. 你好吗?"通过使用此代码
I want to substring the text, ex. "Hello - How are you?" by using this code
$text= "Hello - How are you?";
$strings = explode(' - ',$text);
echo $strings[0]; // Hello
echo $strings[1]; // How are you
由于'-',因此无法正常工作.
it will not work, because of ' - '.
如果我更改为:
$text= "Hello-How are you?";
$strings = explode('-',$text);
echo $strings[0]; // Hello
echo $strings[1]; // How are you
没关系.
有人可以帮助我吗?谢谢.
Can someone help me? Thank you.
推荐答案
我有一个相同的问题,经过一个小时的调查,结果发现从Microsoft Word复制的文本有这样的问题,word选择转换减号( -)到破折号(–),在这里您可以并排查看它们的区别: -
I had the same problem, after an hour of investigating, turns out that texts copied from Microsoft Word have such a problem, word chooses to convert the minus character ( - ) to en dash ( – ) here you can see them side by side to see the difference: -–
我不知道单词为什么会这样,但是我们习惯于看到单词这样令人讨厌的事情,并且您永远不应该让任何人将单词从单词复制到您的应用程序中!
I don't know why word does that, but we are used to see such annoying things done by word and you should never let anyone copy a text from word to you application!
要使您的代码在以下条件下工作,必须首先替换以下奇数字符:
To make you code work in these conditions you must first replace these odd characters:
$text= "Hello-How are you?";
$strings = str_replace('–', '-', $text);
$strings = explode('-',$text);
echo $strings[0]; // Hello
echo $strings[1]; // How are you
这篇关于PHP explode()不适用于" -"特点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!