Jump to content

intext linking specific phrases keywords


dflow

Recommended Posts

<?php
$text="big red apple at microsoft";
$change=array("apple", "microsoft");
$with=array("<a href='http://www.apple.com'>apple</a>", "<a href='http://www.microsft.com'>microsoft</a>");

$changed=str_replace($change, $with, $text);

echo "Original text was $text <br/>";
echo "New text is $changed";
?>

<?php
$text="big red apple at microsoft";
$change=array("apple", "microsoft");
$with=array("<a href='http://www.apple.com'>apple</a>", "<a href='http://www.microsft.com'>microsoft</a>");

$changed=str_replace($change, $with, $text);

echo "Original text was $text <br/>";
echo "New text is $changed";
?>

 

mucho grasias!!!

If you use preg_replace, you can do a case-insensitive search and you can get the code to form the links for you -

 

<?php
$search =  "apple"; // use the | to separate multiple keywords - "red|apple"
$string = "big red apple";

$string = preg_replace("/($search)/i",'<a href="http://\1.com">\1</a>',$string);

echo $string;
?>

<?php
$search =  "proposition|seven|nation|liberty|continent"; // use "red|apple" to match multiple keywords
$string = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.";

$string = preg_replace("/($search)/i",'<a href="http://\1.com">\1</a>',$string);

echo $string;
?>

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.