如何使strpos不区分大小写 [英] How to make strpos case insensitive
问题描述
如何更改strpos以使其不区分大小写.原因是如果product->name
是 MadBike ,而搜索词是 bike ,则该链接不会向我显示.
我主要关心的是代码的速度.
How can I change the strpos to make it non case sensitive. The reason is if the product->name
is MadBike and the search term is bike it will not echo me the link.
My main concern is the speed of the code.
<?php
$xml = simplexml_load_file('test.xml');
$searchterm = "bike";
foreach ($xml->product as $product) {
if (strpos($product->name, $searchterm) !== false ) {
echo $product->link;
} }
?>
推荐答案
您正在寻找 stripos()
You're looking for stripos()
如果您无法使用该功能,则只需先在两个字符串上调用 strtolower()
.
If that isn't available to you, then just call strtolower()
on both strings first.
编辑:
stripos()
将不起作用.
例如:
stripos("Leży Jerzy na wieży i nie wierzy, że na wieży leży dużo JEŻY","jeży");
返回false
,但是它应该返回int(68)
.
stripos("Leży Jerzy na wieży i nie wierzy, że na wieży leży dużo JEŻY","jeży");
returns false
, but it should return int(68)
.
这篇关于如何使strpos不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!