简体   繁体   English

使用php将字符串替换为基于n次出现的单词

[英]Replace a string with a word basing on nth occurence using php

I have a string with anchor tags i filtered the string and got all anchor tags and value. 我有一个带有锚标签的字符串,我过滤了字符串并得到了所有锚标签和值。

my array will be : 我的数组将是:

Array 
(
  [www.google.com] = Array 
      (
        [0]=> click here
        [1]=> search engine
      }
  [http://www.gmail.com] => Array
    (
     [0] => text1
     [1] => 
     [2] => text2
     [3] => 
    )
)

Now i want to replace the link basing on occurrence. 现在我想替换基于发生的链接。 Here 0,1,2 .. represents occurrence of a string. 这里的0,1,2 ..代表字符串的出现。 What should i do if i want to replace 3rd occurrence of gmail link. 如果我想替换第三次出现的Gmail链接,该怎么办。

How to replace a string basing on its occurrence? 如何根据字符串的出现替换字符串? Can anyone help me out of this ? 有人可以帮我吗?

If I am understanding it seems like this would work: 如果我理解的话,这似乎可行:

foreach(array_keys($array_of_anchors) as $anchor_text) {
    if(count($array_of_anchors[$anchor_text]) > 2) {
        $array_of_anchors[$anchor_text][2] = //do replace   
    }
}

A simple loop to search through each anchor, see if there are more than 2 occurrences and then do whatever you want with the 2 index (3rd occurrence) in the array. 一个简单的循环可搜索每个锚点,查看是否有2个以上的匹配项,然后对数组中的2个索引(第3个匹配项)执行所需的操作。

Your question is not very clear, but if you only want to replace the * x *th occurrence of a string you can replace all x occurrences then undo x - 1 . 您的问题不是很清楚,但是如果您只想替换字符串的第* x *次出现,则可以替换所有x次出现,然后撤消x - 1 Assuming $target is your string, $search is what you're looking for, and $replace is the replacement then you could try: 假设$target是您的字符串, $search是您要寻找的, $replace是替换项,则可以尝试:

str_replace($search, $replace, $target, $x); 
str_replace($replace, $search, $target, ($x - 1));

But, be careful, this will go wrong if there was an occurrence of the $replace string already in $target . 但是要小心,如果$target已经存在$target $replace字符串,这将出错。 To be really safe you should check for $replace first. 为了安全起见,您应该先检查$replace This also works for preg_replace . 这也适用于preg_replace

Alternatively you could do preg_match in a loop, counting the matches and moving the offset forward through the string. 或者,您可以在循环中执行preg_match ,对匹配进行计数,并将偏移量向前移动至整个字符串。 Then when the count matches what you want do preg_replace to replace that one. 然后,当计数匹配您想要的计数时,执行preg_replace替换该计数。

If you're working with HTML it might be better to try and parse as a structured document rather than using search/replace. 如果您使用的是HTML,则最好尝试将其解析为结构化文档,而不是使用搜索/替换。

I figured out the solution for my problem instead replacing all search words at once using offset position. 我想出了解决问题的方法,而不是使用偏移位置一次替换所有搜索词。 Its better to replace one after the other in sequential manner which helps in resolving the problem. 最好按顺序替换一个接一个,这有助于解决问题。

protected  function replaceLinkWithShortUrl($content,$toReplace,$search){
        $pos = strpos($content,$search);
        $replacedContent = substr_replace($content, $toReplace, $pos).substr($content, $pos+strlen($search));
        return $replacedContent;
    }

Above code will search for a word in content and replace with a value and append the content again. 上面的代码将在内容中搜索一个单词并替换为一个值,然后再次附加内容。 This procedure will repeat untill replacing all search key's with values. 重复此过程,直到将所有搜索键替换为值。

Thank you guys for those who helped me in resolving the problem. 谢谢你们为我解决问题提供帮助的人。 Thank you SpaceDog.. 谢谢SpaceDog。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM