Jump to content

How to write a function that replaces text


Go to solution Solved by Barand,

Recommended Posts

I'd like to know how to go about writing a function that replaces text in a specific way. Below is my sample code.

<?php
$arrayOfSingleWords = array("Cats", "Dogs", "Horses", "Lions");
$arrayOfDoubleWords = array("blue Cats", "red Dogs", "green Horses", "Lions Nest");
$arrayOfTripleWords = array("blue coller Cats", "Dogs eating food", "green Horses riding", "Yellow Lions roaring");
$arrayOfQuadrupleWords = array("Blue cats napping nearby", "brown Dogs jumping around", "Blue dog Horses standing", "Lions den is blue");

$description = "There once was a dog named sue that lived with some horses and red dogs. The dogs eating food were the " +
				"most like green horses riding. Blue coller cats were also very similar to brown dogs jumping around " +
				"beside the blue cats napping nearby";
?>

I would like to write a function that takes in the $description variable and changes all phrases found in the arrays to links that link somewhere. Perhaps I need an associative array instead to do this, but i'm not sure.

 

I would like the function to start with the longest phrases first(from the $arrayOfQuadrupleWords) and work it's way back to the arrayOfSingleWords. I think some loops are involved but i'm not sure which php built in functions would accomplish this task.

 

The final output should look similar to the following.

 

"There once was a <a href="linksomewhere.php">dog</a> named sue that live with some <a href="linksomewhereelse.php">horses</a> and <a href="linksomewhereyetagain.php">red dogs</a>"

 

and so on.

 

can anyone help me out here on this one? The arrays of words will be pulled from a database, perhaps they would be associative arrays that also include which link to link to,(I think that's how it should work, but not sure, any help appreciated)

I forgot to mention that I'd like the function to start with the array that contains the largest phrases first and replace with links. then move to the array with 3 words and so on. I am hoping that the function could also skip over smaller phrases if that word already has been marked with a link.

 

So for instance, if 'dogs eating food' was already marked when the function rolls over the arrayOfTripleWords, then the word 'dogs' would not be made into a link when the arrayOfSingleWords is rolled over.(in the function)

Edited by FalsAlarm
  • Solution

Use an associative array and strtr()

EG

$links = [   'dogs' => '<a href="link1">dogs</a>',
             'red dogs' => '<a href="link2">red dogs</a>',
             'big red dogs' => '<a href="link3">big red dogs</a>'
         ];
         
$description = "the dogs and the red dogs were being chased by big red dogs";

$newdesc = strtr($description, $links);
  • Like 1
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.