word boundary won't seem to work.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
SchweppesAle
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 11:40 pm

word boundary won't seem to work.

Post 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?
Last edited by Benjamin on Fri May 29, 2009 10:54 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: word boundary won't seem to work.

Post 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.
SchweppesAle
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 11:40 pm

Re: word boundary won't seem to work.

Post 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;
        
        
    }
 
}
 
 
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: word boundary won't seem to work.

Post 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?
SchweppesAle
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 11:40 pm

Re: word boundary won't seem to work.

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: word boundary won't seem to work.

Post 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";
SchweppesAle
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 11:40 pm

Re: word boundary won't seem to work.

Post by SchweppesAle »

perfect,

Thanks a lot, that was actually much easier than I thought it would be.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: word boundary won't seem to work.

Post by prometheuzz »

SchweppesAle wrote:perfect,

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