Thought maybe this would help calrify.
This is the code that has me about 90% of the way there:
I know there are several inefficientcies... any help there would be most grateful. Although im not new to programming, I am brand new to PHP.
Hope this clarifies:
Code: Select all
<?php
function prepare_message_defs($message)
{
//Defaults
$newcontent = "";
//Constant URL tag to the left of word
$left_side="<a href="http://www.samuelsweet.com/board/viewtopic.php?p={URL}&up=1" target="_defs" onclick="window.open('http://www.samuelsweet.com/board/viewtopic.php?p={URL}&up=1', 'definitions', 'height=200, width=500, resize=yes, scrollbars= yes'); return false;" class="postlink">";
//constant URL end tag for the right of word
$right_side ="</a>";
//fake the look up for now-- normally pulled from datasource
$defs=array('id'=>array(5580,5580,5585),'desc' =>array('greenie','bacterial infection','mould'));
$n=count($defs) +1;
//pad out message
$message = ' ' .$message . ' ';
//loop through defs
for ($i=0; $i<$n; $i++) {
$position=0;
do {
//get position of first match
$position=strpos(strtolower($message),strtolower($defs['desc'][$i]),$position);
//is there a match?
if ($position>0){
//swap the {URL} and {Description} placeholders for current rowset
$new_left=str_replace(array("{URL}","{DESCRIPTION}"),array($defs['id'][$i],$defs['desc'][$i]),$left_side);
//Get offset of the length of the description+id+url tag
$description_len=strlen($defs['desc'][$i]);
$id_len=strlen($defs['id'][$i]);
$link_len=strlen($new_left);
$eend=strpos($message,' ',$position);
if ($eend>0){
$eend=$description_len;
}
//Break message up into front, middle, and last
$firstpart=substr($message,0,$position);
$middlepart=substr($message,$position, $eend);
$lastpart=substr($message,($position + $eend));
//check to get the next character
$check=strtolower(substr($lastpart,0,1));
//If next letter is an s... go to the NEXT letter
if ( $check=='s' ){
$check=strtolower(substr($lastpart,1,1));
}
switch ( $check ) {
case ' ':
$check=true;
break;
case '!':
$check=true;
break;
case '?':
$check=true;
break;
case '.':
$check=true;
break;
case ',':
$check=true;
break;
default:
$check=false;
}
//Slap it together and add the diff to $position
if ($check===true){
$message=$firstpart . $new_left . $middlepart . '</a>' . $lastpart;
$position=$position + $link_len + 4;
}else{
$position=$position + $description_len +1;
}
}
} while ($position!==false);
}
$message=trim($message);
return $message;
}
$message="hi ma. Im greenie to get some mould from moUlds some bacterial infections.";
echo('<br />This is the message before processing:<br />' . $message);
$message=prepare_message_defs($message);
echo('<br />Here it is after:<br />' . $message);
echo('<br />What it needs to be is:<br />');
echo('hi ma. Im <a href="http://www.samuelsweet.com/board/viewtopic.php?p=5580&up=1" target="_defs"
onclick="window.open(''http://www.samuelsweet.com/board/viewtopic.php?p=5580&up=1'', ''definitions'', ''height=200, width=500, resize=yes, scrollbars= yes''); return false;"
class="postlink">greenie</a> to get some <a href="http://www.samuelsweet.com/board/viewtopic.php?p=5585&up=1" target="_defs"
onclick="window.open(''http://www.samuelsweet.com/board/viewtopic.php?p=5585&up=1'', ''definitions'', ''height=200, width=500, resize=yes, scrollbars= yes''); return false;"
class="postlink">mould</a> from <a href="http://www.samuelsweet.com/board/viewtopic.php?p=5585&up=1" target="_defs"
onclick="window.open(''http://www.samuelsweet.com/board/viewtopic.php?p=5585&up=1'', ''definitions'', ''height=200, width=500, resize=yes, scrollbars= yes''); return false;"
class="postlink">moUlds</a> some <a href="http://www.samuelsweet.com/board/viewtopic.php?p=5580&up=1" target="_defs"
onclick="window.open(''http://www.samuelsweet.com/board/viewtopic.php?p=5580&up=1'', ''definitions'', ''height=200, width=500, resize=yes, scrollbars= yes''); return false;"
class="postlink">bacterial infections</a>.');
?>