如何在php中查找字符串位置
我想得到$string中每个单词中“ja”的位置,但它不起作用。我做错了什么如何在php中查找字符串位置,php,string,Php,String,我想得到$string中每个单词中“ja”的位置,但它不起作用。我做错了什么 <?php $offset = 0; $find = 'ja'; $find_length = strlen($find); $string = 'jasim jasmin jassir'; while ($string_position = strpos($string, $find, $offer)) { echo $find.' found at '.$string_position.'<br&
<?php
$offset = 0;
$find = 'ja';
$find_length = strlen($find);
$string = 'jasim jasmin jassir';
while ($string_position = strpos($string, $find, $offer)) {
echo $find.' found at '.$string_position.'<br>';
$offset = $string_position + $find_length;
}
?>
$offset
,而将其视为false
while (($string_position = strpos($string, $find, $offset)) !== false) {
$offset
,而将其视为false
while (($string_position = strpos($string, $find, $offset)) !== false) {
试着换盘
<?php
$offset = 1;
$find = 'ja';
$find_length = strlen($find);
$string = 'jasim jasmin jassir';
while ($string_position = strpos($string, $find, $offset)) {
echo $find.' found at '.$string_position.'<br>';
$offset = $string_position + $find_length;
}
?>
// output :- ja found at 6
ja found at 13
然后它将输出3
ja found at 1
ja found at 7
ja found at 14
或尝试更改您的状况检查
while (($string_position = strpos($string, $find, $offset)) !== false) {
试着换盘
<?php
$offset = 1;
$find = 'ja';
$find_length = strlen($find);
$string = 'jasim jasmin jassir';
while ($string_position = strpos($string, $find, $offset)) {
echo $find.' found at '.$string_position.'<br>';
$offset = $string_position + $find_length;
}
?>
// output :- ja found at 6
ja found at 13
然后它将输出3
ja found at 1
ja found at 7
ja found at 14
或尝试更改您的状况检查
while (($string_position = strpos($string, $find, $offset)) !== false) {
也许您应该用这种方式编码来查找字符串位置
$positions = array();
$offset = -1;
while (($pos = strpos($string, $find, $offset+1)) !== false) {
$positions[] = $offset;
}
$result = implode(', ', $positions);
打印此结果也许您应该以这种方式编码以查找字符串位置
$positions = array();
$offset = -1;
while (($pos = strpos($string, $find, $offset+1)) !== false) {
$positions[] = $offset;
}
$result = implode(', ', $positions);
<?php
// stack overflow area
$offset = 0;
$find = 'ja';
$find_length = strlen($find);
$string = 'jasim jasmin jassir';
if (strpos($string, $find) === false) {
echo "not found";
}
else {
while (strpos($string, $find, $offset) !== false) {
$string_position = strpos($string, $find, $offset);
echo $find.' found at '.$string_position.'<br>';
$offset = $string_position + $find_length;
}
}
?>
打印此结果
<?php
// stack overflow area
$offset = 0;
$find = 'ja';
$find_length = strlen($find);
$string = 'jasim jasmin jassir';
if (strpos($string, $find) === false) {
echo "not found";
}
else {
while (strpos($string, $find, $offset) !== false) {
$string_position = strpos($string, $find, $offset);
echo $find.' found at '.$string_position.'<br>';
$offset = $string_position + $find_length;
}
}
?>
解决方法不多,但显然有效
输出:
在0处找到ja
6岁时被发现
ja在13岁时被发现
解决方法不多,但显然有效
输出:
在0处找到ja
6岁时被发现
ja在13岁时被发现
从所示示例中递归使用。检查变量名称(
$offer
不是$offset
)。正如@meze所说:更改为offset仍然不起作用:从所示示例中递归使用。检查变量名称($offer
不是$offset
)。正如@meze所说:更改为offset仍然不起作用:SThanks,这起作用了。我读了一篇关于这个的教程,他没有用==false,为什么他的代码没有这个功能?@user2088624哪个教程?可能是因为他的字符串不是从你要找的子字符串开始的。啊,现在我明白了,非常感谢。我会在可能的时候接受你的回答。谢谢,这很有效。我读了一篇关于这个的教程,他没有用==false,为什么他的代码没有这个功能?@user2088624哪个教程?可能是因为他的字符串不是从你要找的子字符串开始的。啊,现在我明白了,非常感谢。如果可以的话,我会接受你的回答。如果偏移量=1,你会错过“ja”的第一次出现。如果偏移量=1,你会错过“ja”的第一次出现