FalsAlarm Posted November 27, 2017 Share Posted November 27, 2017 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) Quote Link to comment https://forums.phpfreaks.com/topic/305770-how-to-write-a-function-that-replaces-text/ Share on other sites More sharing options...
FalsAlarm Posted November 27, 2017 Author Share Posted November 27, 2017 (edited) 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 November 27, 2017 by FalsAlarm Quote Link to comment https://forums.phpfreaks.com/topic/305770-how-to-write-a-function-that-replaces-text/#findComment-1554153 Share on other sites More sharing options...
Solution Barand Posted November 27, 2017 Solution Share Posted November 27, 2017 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); 1 Quote Link to comment https://forums.phpfreaks.com/topic/305770-how-to-write-a-function-that-replaces-text/#findComment-1554156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.