Home  >  Article  >  Backend Development  >  Regarding how str_replace_once only replaces the content but not the alt tag in

Regarding how str_replace_once only replaces the content but not the alt tag in

WBOY
WBOYOriginal
2016-09-09 08:27:572456browse

About str_replace_once($needle,$replace,$haystack), how to replace only the content without replacing the alt and title tags in

The original code is as follows
Code 1:

<code>public function str_replace_once($needle,$replace,$haystack) 
{
    $pos = strpos($haystack, $needle);
 
    if ($pos === false) 
    {
        return $haystack;
    }
     
    return substr_replace($haystack, $replace, $pos, strlen($needle));
}
</code>

Please help me how can I replace only the text content when replacing characters, but not the alt and title characters in the image

My purpose is, there is an article $str, and then there is an array $arr=array("小草","小花","狗","Willow"); I want to include all the array contents in the article. Replace it with something like Xiaocaohuahua, which is equivalent to an internal link in the article, but when encountering
Xiaocao//does not operate
aaa//does not operate

aa

//Do not operate
...
Although there are contents containing arrays, they are not replaced.

Reply content:

About str_replace_once($needle,$replace,$haystack), how to replace only the content without replacing the alt and title tags in

The original code is as follows
Code 1:

<code>public function str_replace_once($needle,$replace,$haystack) 
{
    $pos = strpos($haystack, $needle);
 
    if ($pos === false) 
    {
        return $haystack;
    }
     
    return substr_replace($haystack, $replace, $pos, strlen($needle));
}
</code>

Please help me how can I replace only the text content when replacing characters, but not the alt and title characters in the image

My purpose is, there is an article $str, and then there is an array $arr=array("小草","小花","狗","Willow"); I want to include all the array contents in the article. Replace it with something like Xiaocaohuahua, which is equivalent to an internal link in the article, but when encountering
Xiaocao//does not operate
aaa//does not operate

aa

//Do not operate
...
Although there are contents containing arrays, they are not replaced.

String replacement is no longer enough for you, please try regular expressions.

Or if you want to process tags, you can try those plug-ins for xml parsing

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn