带多维数组的 stripos
我正在尝试制定一个 php 函数来搜索引用页面中的术语,然后根据这些术语的存在执行一个函数。
创建基本代码不是问题,但由于单词和可选操作的数量相当大,脚本变得相当长,为每组单词/函数使用单独的行。基本代码概念如下。 stripos 函数的优先顺序相反,因此,如果出现两个或多个术语,则最重要的术语位于最后,并且将覆盖前面的术语
(我想可能有一种方法可以在第一个术语之后退出脚本)条件满足了,但是我的退出实验失败了,所以我就用了逆序)。
group1 = array("word1","word2","word3","word4","word5");
group2 = array("word6","word7","word8");
group3 ... etc
foreach($group1 as $groupa) { if(stripos($string, $groupa) !== false) { do something A; } }
foreach($group2 as $groupb) { if(stripos($string, $groupb) !== false) { do something B; } }
foreach ... etc
有没有办法使用二维数组或两个数组,一个包含单词,一个包含动作?即:
words = array("word1","word2","word3","word4","word5","word6","word7","word8")
actions = array("something A","something A","something A","something A","something A","something B","something B","something B")
foreach($words as $word) { if(stripos($string, $word) !== false) { do actions; } }
……更新……
受菲利普斯建议的启发,我们最终得到了一个多维数组,然后逐步浏览它的“行”。现在正致力于从 MySQL 获取数组,而不是用代码写出来。
$terms = array(
array( "word" => "word1",
"cat" => "dstn",
"value" => "XXXX"
),
..etc
..etc
);
foreach ($terms as $i => $row)
{ if(stripos($refstring3, $row['word']) !== false) { $$row['cat'] = $row['value']; } }
……更新了……
它已经演变成一个简单的 MySQL 查询,后面跟着一个 while 语句而不是 foreach。感谢 Stackoverflow 上的反馈和其他各种帖子,它的作用就像一个魅力。
感谢大家。
这个地方非常适合学习和理解,帖子直接跳到事物的本质,而不必搜索大量相关但不适用的教程。
I am trying to work out a php function to search the referring page for terms and then perform a function based on the existence of those terms.
Creating the basic code wasn't an issue, but with a fairly large number of words and optional actions, the script is getting quite long using individual lines for each group of words/function. The basic code concept is below. The stripos functions are in reverse order of preference, so that if two or more terms appear, then the most important ones are last and will over ride the previous ones
(I imagine there maybe a way to exit the script after the first condition is met, but my experiments with exit failed, so I just used reverse sequencing).
group1 = array("word1","word2","word3","word4","word5");
group2 = array("word6","word7","word8");
group3 ... etc
foreach($group1 as $groupa) { if(stripos($string, $groupa) !== false) { do something A; } }
foreach($group2 as $groupb) { if(stripos($string, $groupb) !== false) { do something B; } }
foreach ... etc
Is there a way to use a two dimensional array or two arrays, one with words and one with action ? ie:
words = array("word1","word2","word3","word4","word5","word6","word7","word8")
actions = array("something A","something A","something A","something A","something A","something B","something B","something B")
foreach($words as $word) { if(stripos($string, $word) !== false) { do actions; } }
...... UPDATED ......
Inspired by Phillips suggestion, we ended up with a multidimensional array and then stepped through its "rows". Now working on fetching the array from MySQL rather than writing it out in code.
$terms = array(
array( "word" => "word1",
"cat" => "dstn",
"value" => "XXXX"
),
..etc
..etc
);
foreach ($terms as $i => $row)
{ if(stripos($refstring3, $row['word']) !== false) { $row['cat'] = $row['value']; } }
...... UPDATED ......
It has evolved to a simple MySQL query, followed by a while statement rather than a foreach. Works like a charm, thanks to feedback and various other posts on Stackoverflow.
Thanks to all.
This place is great for learning and understanding, posts jump straight to the meat of things and skip having to search through numerous related but inapplicable tutorials.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将单词操作存储为键值数组,
然后遍历这些数组并使用 Eval 和字符串连接来调用函数: http://php.net/manual/en/function.eval.php
但是,如果您告诉我们更多有关您实际想要实现的目标 - 即什么是您想要采取的行动的一些示例,基于以下内容 字? -- 可能有更好、更简洁的方法来组织代码。请记住,Eval 需要通过从不传递用户内容来确保安全,因此只能使用您自己的“白名单”命令。
You could store your word-actions as a key-value array in the form of
then go through these and use Eval and string concatenation to call the function: http://php.net/manual/en/function.eval.php
However, if you tell us more about what you actually want to achieve -- i.e. what are some examples of actions you want to take, based on what words? -- there may be much better and cleaner ways to organize your code. And keep in mind that Eval needs to be secured by never passing it user content, so only work with your own "whitelisted" commands.
尽管 Philipp Lenssen 给出的答案肯定是正确的(并且被接受)并且会起作用,但我真的不喜欢使用 eval 来执行函数的想法。你可以试试这个;
确实没有必要使用 eval,我强烈建议不要使用它。
Although the answer Philipp Lenssen gave is certainly correct (and accepted) and will work, I really dislike the thought of using eval to perform a function. You can try this instead;
There really is no need to use eval and I would strongly advise against it.