Jump to content

[SOLVED] setting word boundaries with regex


SchweppesAle

Recommended Posts

hi, my problem is that the following code isn't pulling individual words but rather any set of matching characters.  How can I correct the following

 

$content = $article -> text;

	$keywords = $this -> params->def('keywords');
	$words = explode(",", $keywords);

	$keywordLinks = $this -> params->def('links');
	$Links = explode(",", $keywordLinks);


$number = count($words);



for($x = 0; $x <= count($words); $x++)
{
	if(preg_match_all("/\b".$words[$x]."\b/i", $content, $matches))
	{	
			foreach($matches[0] as $change)
			{
			$content = str_replace($change, str_replace($words[$x], '<a href = "'.$Links[$x].'">'.$words[$x].'</a>', $change), $content);
			}
	}
}




	$article -> text = $content;

 

 

$content = $article -> text;

	$keywords = $this -> params->def('keywords');
	$words = explode(",", $keywords);

	$keywordLinks = $this -> params->def('links');
	$Links = explode(",", $keywordLinks);


$number = count($words);



for($x = 0; $x <= count($words); $x++)
{
	if(preg_match_all("/(".$words[$x].")+/i", $content, $matches)) //changes it to  parenthesis and removed /b's
	{	
			foreach($matches[0] as $change)
			{
			$content = str_replace($change, str_replace($words[$x], '<a href = "'.$Links[$x].'">'.$words[$x].'</a>', $change), $content);
			}
	}
}




	$article -> text = $content;

 

maybe?

still no good.  I'll actually test the pattern by using the word "black" then run it through a string with the following output:

 

blackwhitered

 

>black<

 

blackblackblack

 

black.

 

 

Every one of them is being picked up though.  Am I doing this wrong? :/

was actually very simple

 

public function onPrepareContent(&$article, &$params, $limitstart)
{		
	global $mainframe;

	$content = $article -> text;

	$keywords = $this -> params->def('keywords');
	$words = explode(",", $keywords);

	$keywordLinks = $this -> params->def('links');
	$Links = explode(",", $keywordLinks);



$number = count($words);

for($i = 0; $i < $number; $i++)
{
$Links[$i] = '<a href = "'.$Links[$i].'">'.$words[$i].'</a>';
/*$Links[$i] = str_replace(' ', '', $Links[$i]);*/
}

for($i = 0; $i < $number; $i++)
{
$words[$i] = '/\b'.$words[$i].'\b/';
$words[$i] = str_replace(' ', '', $words[$i]);
}

$content  = preg_replace($words, $Links, $content);


$article -> text = $content;

preg_replace, done. lol  ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.