I am working on a poetry site powered by WordPress. I want to convert a string (actually post content) into array to add a link to each word to search other poems into my site database having same word. What I have worked so far is:
<?php
//sample poem
$str = 'We two, how long we were fool’d,
Now transmuted, we swiftly escape as Nature escapes,
We are Nature, long have we been absent, but now we return,
We become plants, trunks, foliage, roots, bark,
We are bedded in the ground, we are rocks,
We are oaks, we grow in the openings side by side,
We browse, we are two among the wild herds spontaneous as any,';
$arr = preg_split("/[\s,]+/", $str);
foreach ($arr as $poemarr) {
$poem = "<a href = https://www.google.com>" . $poemarr . "</a>"; //sample google link but actually want to add WordPress filter to search other poems having same word.
echo $poem . " "; //here what should I do to print the string to look like exactly the same as $str???
}
?>
Output of the code is:
We two how long we were fool’d Now transmuted we swiftly escape as Nature escapes We are Nature long have we been absent but now we return We become plants trunks foliage roots bark We are bedded in the ground we are rocks We are oaks we grow in the openings side by side We browse we are two among the wild herds spontaneous as any
This code is working fine but I want that array should be printed exactly like original poem. Any help or direction will be appreciated a lot.
This should feel your needs, pleases give it a try
<?php
//sample poem
$str = 'We two, how long we were fool’d,
Now transmuted, we swiftly escape as Nature escapes,
We are Nature, long have we been absent, but now we return,
We become plants, trunks, foliage, roots, bark,
We are bedded in the ground, we are rocks,
We are oaks, we grow in the openings side by side,
We browse, we are two among the wild herds spontaneous as any,';
$words = preg_split("/[\s,]+/", $str);
$wordToLinks = [];
foreach ($words as $word) {
$wordUsages = ''; // Here do your wp queries to get usage of the word
$wordLink = 'sprintf('<a href=%s>%s</a>', $wordUsages, $word); //sample google link
$wordToLinks[$word] = $wordLink;
}
// Replace the word in the text (no ',' or '\t' ..)
// By the link to the word (please replace by your research link)
echo str_replace(array_keys($wordToLinks), array_values($wordToLinks), $str);
I don't know why you need to split it into an array. You can just use preg_replace to wrap all the instances in a link.
This will search for any string up until, but not including a space a comma or a period. Then wrap the same word in the link.
<?php
$str = <<<EOD
We two, how long we were fool’d,
Now transmuted, we swiftly escape as Nature escapes,
We are Nature, long have we been absent, but now we return,
We become plants, trunks, foliage, roots, bark,
We are bedded in the ground, we are rocks,
We are oaks, we grow in the openings side by side,
We browse, we are two among the wild herds spontaneous as any
EOD;
$str = preg_replace("/([^ ,.
]+)/", '<a href="https://www.google.com/search?q=$1">$1</a>', $str);
Echoing at this point will have the lines separated with line breaks ( ). To have the lines separated with <br />
tags you'd need to convert them
echo nl2br($str);