I'm building a tag system.
I want it to check some text. Then check if the text contains any of terms listed in an array. If it does than output the corresponding text listed in another array? (Not sure if this is how it should be done)
so far..
<?php
$information = "This is the sentence that will be checked 123 hello world happy";
$c开发者_开发百科heck = 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. " ";
}
?>
where it says $name in the statement is where I would want it to output the corresponding word. the reason I am doing it this way is because there will be a few variations of a similar phrase that I would like to output as a single...
eg
"happy", "cheerful", "good mood" would all output as "happy"
Try this approach:
$check = array( 'happy' => ".*(happy|good mood|cheerful).*" ) foreach ($check as $key => $value) { $matches = array(); if (preg_match('/'.$value.'/i',$information, &$matches)) { ... } }
Change the array declarations and the loop like so:
$check = array(
'hello' => 'bonjour',
'1' => 'one',
'234' => 'twothreefour',
'andy' => 'andrew',
'happy' => 'happy',
'good mood' => 'happy',
);
foreach ($check as $value => $name)
The rest of the code remains unchanged. I would choose different variable names, but this is the minimal change needed.
精彩评论