Page 1 of 1

word boundary won't seem to work.

Posted: Fri May 29, 2009 1:31 am
by SchweppesAle
hi, I'm using the following pattern in order to append links to specific words within an array. However the pattern I'm using seems to completely ignore the word boundary.

Code: Select all

 
$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);
                }
        }
    }
 
any ideas?

Re: word boundary won't seem to work.

Posted: Fri May 29, 2009 2:06 am
by prometheuzz
Hard to say without seeing the input string. Can you post it?
Also post what the desired result should be and what result you are currently getting.

Re: word boundary won't seem to work.

Posted: Mon Jun 01, 2009 11:25 am
by SchweppesAle
prometheuzz wrote:Hard to say without seeing the input string. Can you post it?
Also post what the desired result should be and what result you are currently getting.
sure thing. Here's the Input String:

Code: Select all

 
<p> test</p><p>test</p><p>blah</p><p>ok</p><p>test test >test< blah</p><p>blahokblah </p><p>&nbsp;</p><p>blahsirdir</p><p>blabergasted</p><p>backok </p>
 
output:

Code: Select all

 
<p> <a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com">test</a></a></a></a></a></p><p><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com">test</a></a></a></a></a></p><p><a href = "http://www.pmgb2b.com"><a href = "http://www.pmgb2b.com">blah</a></a></p><p><a href = "http://www.yahoo.com"><a href = "http://www.yahoo.com">ok</a></a></p><p><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com">test</a></a></a></a></a> <a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com">test</a></a></a></a></a> ><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com"><a href = "http://www.theprogressiveaccountant.com">test</a></a></a></a></a>< <a href = "http://www.pmgb2b.com"><a href = "http://www.pmgb2b.com">blah</a></a></p><p><a href = "http://www.pmgb2b.com"><a href = "http://www.pmgb2b.com">blah</a></a><a href = "http://www.yahoo.com"><a href = "http://www.yahoo.com">ok</a></a><a href = "http://www.pmgb2b.com"><a href = "http://www.pmgb2b.com">blah</a></a> </p><p> </p><p><a href = "http://www.pmgb2b.com"><a href = "http://www.pmgb2b.com">blah</a></a>sirdir</p><p>blabergasted</p><p>back<a href = "http://www.yahoo.com"><a href = "http://www.yahoo.com">ok</a></a> </p> 
 
as you can see, it's applying the links to each pattern multiple times, which was not what I was trying to do :P


and here's the code which I'm working on.

Code: Select all

 
class plgContentLinkplugin extends JPlugin
{
    
    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($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;
        
        
    }
 
}
 
 

Re: word boundary won't seem to work.

Posted: Mon Jun 01, 2009 12:35 pm
by prometheuzz
Okay, you posted the input string, posted the actual output but forgot to post the desired output.
Sorry, but the code you posted does not mean much to me. I only know some regex, PHP itself is beyond my scope.

So given the input:

Code: Select all

<p> test</p><p>test</p><p>blah</p><p>ok</p><p>test test >test< blah</p><p>blahokblah </p><p>&nbsp;</p><p>blahsirdir</p><p>blabergasted</p><p>backok </p>
what should be the output?

Re: word boundary won't seem to work.

Posted: Mon Jun 01, 2009 1:12 pm
by SchweppesAle
sorry, I'd like to see the following

Code: Select all

 
<p><a href = "http://www.theprogressiveaccountant.com/">test</a></p><p><a href = "http://www.pmgb2b.com/">blah</a></p><p><a href = "http://www.yahoo.com/">ok</a></p>
 
etc etc. basically I have two arrays and a string. It should cycle through one array and test it against the current string, each time a pattern is found the algorithm will check the associated key of the second array which stores the url then append it to that pattern.


So
Array1[0] = test
Array2[0] = http://www.theprogressiveaccountant.com

this link should be appended to any matches of the word test within the string. Unfortunately, I'm having trouble isolating individual words within the string and those which aren't already linked - which I think might be causing the same link to be appended numerous times to each pattern.

Re: word boundary won't seem to work.

Posted: Mon Jun 01, 2009 1:25 pm
by prometheuzz
Ah, I see where you're going.
Note that the preg_replace(...) function can take an array of regex patterns and an array of replacement values. This is probably what you're trying to do:

Code: Select all

$text = '<p>test</p><p>test</p><p>blah</p><p>ok</p><p>test test >test< blah</p><p>blahokblah</p><p>&nbsp;</p><p>blahsirdir</p><p>blabergasted</p><p>backok</p>';
$words = array('/\btest\b/', '/\bblah\b/', '/\bok\b/');
$replacements = array('<a href="http://www.a.com">test</a>', '<a href="http://www.b.com">blah</a>', '<a href="http://www.c.com">ok</a>');
echo "$text\n";
$text = preg_replace($words, $replacements, $text);
echo "$text\n";

Re: word boundary won't seem to work.

Posted: Mon Jun 01, 2009 4:11 pm
by SchweppesAle
perfect,

Thanks a lot, that was actually much easier than I thought it would be.

Re: word boundary won't seem to work.

Posted: Mon Jun 01, 2009 4:59 pm
by prometheuzz
SchweppesAle wrote:perfect,

Thanks a lot, that was actually much easier than I thought it would be.
You're welcome.