duangai9678 2018-09-12 14:55
浏览 54

将字符串转换为数组,然后在向每个单词添加过滤器链接后将数组打印为看起来像原始字符串

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.

  • 写回答

2条回答 默认 最新

  • douchuose2514 2018-09-12 15:12
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值