我保存为| 1 | 7 | 11 | 我需要使用preg_match来检查| 7 |有没有或| 11 |在那里等,我该怎么做?PHP Preg_match匹配确切单词
3
A
回答
2
0
如果你真的想使用preg_match
(尽管我建议strpos
,就像Xeoncross的回答),使用此:
if (preg_match('/\|(7|11)\|/', $string))
{
//found
}
0
假设你的字符串总是启动并与|
结束:
strpos($string, '|'.$number.'|'));
17
使用\b
表达式前后仅匹配它作为一个整词:
$str1 = 'foo bar'; // has matches (foo, bar)
$str2 = 'barman foobar'; // no matches
$test1 = preg_match('/\b(foo|bar)\b/', $str1);
$test2 = preg_match('/\b(foo|bar)\b/', $str2);
var_dump($test1); // 1
var_dump($test2); // 0
所以,在你的榜样,那就是:
$str1 = '|1|77|111|'; // has matches (1)
$str2 = '|01|77|111|'; // no matches
$test1 = preg_match('/\b(1|7|11)\b/', $str1);
$test2 = preg_match('/\b(1|7|11)\b/', $str2);
var_dump($test1); // 1
var_dump($test2); // 0
相关问题
- 1. PHP匹配整个单词
- 2. 正则表达式设置单词字符和匹配确切的单词
- 3. MongoDB全文搜索 - 匹配单词和确切短语
- 4. 如何通过SPARQL匹配一个确切的单词
- 5. preg_match不匹配
- 6. SQL精确匹配列中的单词
- 7. grep匹配一个单词,但不匹配另一个单词
- 8. 匹配preg_match中的“---”
- 9. 的preg_match不匹配
- 10. PHP preg_match只有一个匹配
- 11. PHP preg_match找到多个匹配
- 12. PHP的preg_match匹配的文本
- 13. Elasticsearch匹配确切项
- 14. Algolia匹配确切属性
- 15. ThinkingSphinx匹配确切短语
- 16. 使用preg_match检查单词
- 17. 的preg_match匹配多个匹配
- 18. 结构匹配的preg_match
- 19. 在java中使用正则表达式匹配确切词
- 20. 正则表达式匹配确切的词
- 21. 使用正则表达式匹配数字和单词的确切数量 - Python27
- 22. Laravel用RLIKE查询以匹配字符串中的确切单词
- 23. PHP preg_match找到某个词
- 24. 如何在java中使用模式匹配器找到匹配的确切单词
- 25. 搜索字符串中的单词以确切匹配/甚至开始时该单词不起作用
- 26. 如何正则表达式匹配一切,但长单词?
- 27. 如何剪切从句子集合中匹配的单词?
- 28. 匹配的一切,但一个特定的单词
- 29. preg_match匹配特定字符= {} [] *
- 30. 的preg_match部分匹配
这也将像17,27,71和111,211和114号仅举几例 – Yeroon 2010-11-29 15:22:16
谢谢返回TRUE,这是一个更简单的方法和工作! – ITg 2010-11-29 15:22:51