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.