2011-02-15 124 views
0

我正在构建标记系统。从一个阵列到另一个阵列的交叉引用标记系统

我希望它检查一些文本。 然后检查文本是否包含数组中列出的任何术语。 如果它输出的是另一个数组中列出的相应文本? (不知道这是应该做)

到目前为止..

<?php 
$information = "This is the sentence that will be checked 123 hello world happy"; 

$check = array('hello','1','234','andy','happy', 'good mood'); 
$name = array ('bonjour','one','twothreefour','andrew','happy','happy'); 

foreach ($check as $value) 

if (preg_match("/".$value."/i",$information)) { 
     $output = "<a href='/search/result/?q=".$value."'>".$name."</a>"; 
     print $output. " "; 
    } 
?> 

它在声明中说$ name是我希望它输出的对应词。我做这种方式的原因是因为会有,我想作为一个单一的输出类似短语的一些变化......

“快乐”,“愉快的”,“好心情”将所有输出作为‘快乐’

回答

1

试试这个办法:

 
$check = array(
    'happy' => ".*(happy|good mood|cheerful).*" 
) 
foreach ($check as $key => $value) { 
    $matches = array(); 
    if (preg_match('/'.$value.'/i',$information, &$matches)) { ... } 
} 
+0

完美谢谢 – AJFMEDIA 2011-02-15 09:27:39

1

改变数组声明和循环,像这样:

$check = array(
     'hello' => 'bonjour', 
     '1' => 'one', 
     '234' => 'twothreefour', 
     'andy' => 'andrew', 
     'happy' => 'happy', 
     'good mood' => 'happy', 
    ); 

foreach ($check as $value => $name) 

其余代码保持不变。我会选择不同的变量名称,但这是需要的最小更改。