Hello!Somebody can help me please how can i autolink any word i have in my webpage autolink in php ?
example array

$word_list = array  (

    'dogs' => 'http://dogs.com', 
    'dog' => 'http://dogs.com', 
    'cat' => 'http://cats.com', 
    'kitten' => 'http://cats.com',
    'horse' => 'http://horses.com'
    )

any text dogs in my website corispond to link <a href="http://dogs.com">dogs</a>

Thank you in advance.

preg_replace is a good option. Where does the text come from?

I need in all articles have words like in array replace with links,make that tag link

Member Avatar
LastMitch

I need in all articles have words like in array replace with links,make that tag link

I assume you got the code snippet from here:

http://coderzone.org/library/PHP-Auto-link-text-with-a-given-set-of_1085.htm

Like what pritaeas mention used preg_replace function.

I copy and paste the code from the link so it would be easier to follow:

// list of keywords to auto-link 
// list plural forms first
$reserved_word_list = array (
    'dogs' => 'http://dogs.com', 
    'dog' => 'http://dogs.com', 
    'cat' => 'http://cats.com', 
    'kitten' => 'http://cats.com',
    'horse' => 'http://horses.com'
}

// search text string and auto-link the words
foreach($reserved_word_list as $word => $rep_string){

if(strpos($some_text, $word)){

// link the word
$some_text = preg_replace('/(\s+)('.preg_quote($word).')/i','$1<a href="'.$rep_string.'">$2</a>',$some_text);

}
}

As you can see it does have preg_replace() function.

You can pass arrays to preg_replace:

$some_text = preg_replace(array_keys($reserved_word_list), array_values($reserved_word_list), $some_text);

But then you need to use correct patterns in your array:

$reserved_word_list = array (
    '/\bdog(s)?\b/' => 'http://dogs.com', 
    '/\bcat|kitten\b/' => 'http://cats.com', 
    '/\bhorse\b/' => 'http://horses.com'
}

Thank you for your time ,but i see i am not explaing very well . i do an example for that

<?php 
    $reserved_word_list = array (
    '/\bdog(s)?\b/' => 'http://dogs.com',
    '/\bcat|kitten\b/' => 'http://cats.com',
    '/\bhorse\b/' => 'http://horses.com'
    );
    $some_text = preg_replace(array_keys($reserved_word_list), array_values($reserved_word_list), $some_text);
    if (isset($some_text)) {
?>

bcat bdog  bhorse cat horse dog
<?php } ?>

In this code i need linking auto my words --> bcat bdog bhorse cat horse dog

Thank you another time.

Member Avatar
LastMitch

Thank you for your time ,but i see i am not explaing very well . i do an example for that

I feel English might not be your first language.

pritaeas already mention that:

But then you need to use correct patterns in your array

Another words he did most of the work and you need to test it out and find the correct pattern.

If you still having problems figuring it out then I would suggested you should pay pritaeas at least 30 euros = 30€

He would gladly finish the code to suited your version.

If you want me to do it I would charge 40 dollars = $40

Try this:

<?php
$text = 'The quick brown fox jumps over the lazy white dog and the small blacks dogs.';

$replaceList = array (
    '/\b(dog|dogs)\b/' => '<a href="http://dog.com">$1</a>',
    '/\b(fox)\b/' => '<a href="http://fox.com">$1</a>'
);

$newText = preg_replace(array_keys($replaceList), array_values($replaceList), $text);
echo $newText;
?>

Thank you pritaeas very appreciated.