【问题标题】:PHP , Search and Replace array element inside string [closed]PHP,搜索和替换字符串内的数组元素[关闭]
【发布时间】:2021-12-05 07:51:07
【问题描述】:

我尝试在字符串中搜索和替换数组元素,但不知道该怎么做! 我的代码前:

$string_text = 'some random text with random lenght ';

function check_strings($string_text){

$new_string_text = $string_text; //random text with randon lenght 
$array_of_3000 = ['some','text','hello','no','why','oh','random']; // array with lenght of 3000
$text_tokens = explode($new_string_text,' '); // to make it array 
foreach($text_tokens as $token_index =>$token ){  // loop on $text_tokens 
    if (in_array($token, $array_of_3000)){   // if token in $array_of_3000 -> change token with link 

        $token_link = sprintf("<a href='<?php echo site_url('/%s'); ?>' >  %s </a>",$tokens,$tokens); // changeing $token to  html link 
        // update $new_string_text , $token with -> $token_link 
        $new_string_text = str_replace($tokens,$token_link,$tokens);


    }
}
    return $new_string_text;
}
 //  text =  'some random text with random lenght ' -> same , text , random x2 are in $array_of_3000 so need to change them with html link 
    // random x2 -> if find more as 1 just link 1 of them 
    // " <a href='<?php echo site_url('/some'); ?>' >  some </a>"
    // " <a href='<?php echo site_url('/text '); ?>' >  text </a>"
    // " <a href='<?php echo site_url('/random '); ?>' >  random </a>" 
// output -> all words in input string if can finded in $array_of_3000 need change with it link 
   
   

if 语句 (in_array()) 不起作用,我不知道如何找到匹配词 我该怎么做 ? 感谢您的帮助:)

感谢我修复它的每一个人 代码现在可以正常工作了```

        if (in_array($token, $array_of_words)){   // if token in $array_of_3000 -> change token with link 
    
            $token_link = sprintf("<a   href='<?php echo site_url('/%s'); ?>' >  %s </a>",$tokens,$tokens); // changeing $token to  html link 
            // update $new_string_text , $token with -> $token_link 
            $new_string_text = str_replace($tokens,$token_link,$tokens);
    
    
        }
    }

【问题讨论】:

  • 请提高您问题的清晰度。此外,您可以添加您想要实现的输入 -> 输出示例。我不清楚你想做什么。
  • @Gass 谢谢,我尽量让它更清楚,我有一些随机文本和 3000 个单词的数组。如果任何数组元素在文本中,请使用链接更改它们
  • 给定的代码有什么不工作的地方吗?如果是,请分享有关您面临的问题的更多详细信息
  • @NicoHaase 是,如果语句不起作用。首先我需要检查字符串是否有任何数组将其更改为 html 链接
  • 请通过编辑为您的问题添加所有说明。 究竟是什么不起作用?你有什么努力让它发挥作用?

标签: php arrays string if-statement replace


【解决方案1】:

可以使用phpstr_replace (php manual)函数

str_replace 接受参数中的数组

示例:

$str= 'random text idk';
$tokens = ['random', 'idk'];
$newTokens = ['token1', 'token2'];

$str = str_replace($tokens, $newTokens, $str));
// value of str: "token1 text token2"

对于您的示例,您可以使用它

function check_strings($string_text){
    $array_of_3000 = ['some', 'text', 'hello', 'no', 'why', 'oh', 'random'];
    $array_of_3000_replacements = [];
        
    foreach ($array_of_3000 as $oldToken) {
        $array_of_3000_replacements[] = sprintf("<a href='<?php echo site_url('/%s'); ?>' >  %s </a>", $oldToken, $oldToken);
    }

    return str_replace($array_of_3000, $array_of_3000_replacements, $string_text);
}

【讨论】:

  • 嘿,谢谢你的回答,我试试这个,但我不知道为什么它不起作用在循环中查找
  • 为什么需要循环?只需给出 oldTokens 和 newTokens 的 str_replace 数组,里面的函数就会循环遍历这些标记来替换所有
  • 如果单词在数组中,我需要用数组元素检查文本中的每个单词也需要使用链接更改单词,所以我认为我需要在文本循环内执行此操作并为每个单词创建新链接
  • 你要做的正是函数正在做的事情,把你的话放在一个数组中,把链接放在另一个数组上,然后 $yourFinalText = str_replace($words, $links, $string )如果字符串中存在数组 words 中的任何单词,它将被链接数组中的等效链接替换(仔细阅读我在答案中放置的示例)
猜你喜欢
  • 2019-11-15
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 2018-03-09
相关资源
最近更新 更多