简体   繁体   English

得到没有输出的同时在PHP循环?

[英]getting no output for while loop in php?

I have been trying to take out some value from a string in php but not getting success so if you people can please take a look at my code as : 我一直在尝试从php中的字符串中提取一些值,但是没有成功,因此,如果您可以,请看一下我的代码:

$html = "[ect]afdbec[ect]";
$output =true;
$x=1;

while ($output == true) {
$pos = strpos($html,"[");
$value = substr($html,$pos,$x);
echo $value;
$x++;
if ($value == "]") { $output = false; }
}

I think your issue might be that you're checking when $value is equal to "]" (in the if-statement), but $value will never be just one character; 我认为您的问题可能是您正在检查$value何时等于“]”(在if语句中),但是$ value永远不会只是一个字符; you're asking it to always substr() from the position of the first "[". 您要求它始终从第一个“ [”的位置开始substr()

You might want to have a look at preg_match() - there's a slight learning curve if you're new to it, but it'll help with this situations in future development. 您可能想看一下preg_match() -如果您不熟悉它,则会有一条学习曲线,但是它将在将来的开发中帮助解决这种情况。

Put the line $pos = strpos($html,"["); $pos = strpos($html,"["); outside the while loop. while循环之外。 Because, it initializes the value of $pos to the position of "[" in each iteration of the loop. 因为,它将在循环的每次迭代中将$pos的值初始化为"["的位置。 I don't think it will solve this problem. 我认为这不会解决这个问题。

Please get used to preg_match() . 请习惯于preg_match() There will be a lot more simple way to do it. 会有很多更简单的方法来做到这一点。

Please try: 请试试:

$html = "[ect]afdbec[ect]";
preg_match('~\[(.*?)\]~', $html, $match);
echo $match[1];

Please look here also 也请看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM