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";
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.