如何从字符串到echo变量获取搜索结果

时间:2015-02-20 16:35:04

标签: php

我正在尝试搜索一个包含多个术语的字符串,如果找到,那么相对术语将回显给我可以使用的变量,此时它只是回显最后一个类别而不管它发现了什么:

//Page info

$PageTitle = "audio";

$search_for = array('Electrical Serviceh' => 'electrical, audio, electrician, security', 'Plumbing Servicey' => 'heating, gas');
foreach ($search_for as $name=>$term){
 if (strpos($PageTitle,$term) !== false){
 echo $name;

 }
}

echo $name;

需要能够通过回显它在页面的任何地方使用输出的变量。

1 个答案:

答案 0 :(得分:1)

让它发挥作用。我重新定义了数组并添加了第二个foreach循环

    <?php
    //Page info
    $PageTitle = "Johnson electrician";
    $search_for =
    array('Electrical Serviceh' => array('electrical', 'audio', 'electrician', 'security'), 'Plumbing Servicey' => array('heating', 'gas'));
    foreach ($search_for as $name=>$term){
         foreach ($term as $findme)
         {
             if (strpos($PageTitle,$findme) !== false)
             {
             echo $name;
             }
        }
    }
    ?>