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;

 

 

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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? :/

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.