Home Backend Development PHP Problem How to replace php keywords

How to replace php keywords

Jul 29, 2020 am 10:21 AM

php keyword replacement method: 1. Use the "preg_replace" function to realize the keyword replacement function, and you can control the number of substitutions; 2. Use the "substr_replace" function to realize the keyword replacement, but only One replacement.

How to replace php keywords

Recommended: "PHP Tutorial"

php keyword replacement

(1) Use the preg_replace function to implement this function. Because the preg_replace function itself can control the number of replacements, I thought of it from the beginning. The specific implementation method is as follows:

//可以实现替换次数的控制,不仅限于只替换一次,比如$limit为2的时候表示一个词出现很多吃的时候仅替换2次,-1表示全部替换。$search 和 $replace 都可以是字符串或者数组,但必须对应 
function str_replace_limit($search,$replace,$content,$limit=-1){ 
  if(is_array($search)){ 
    foreach ($search as $k=>$v){ 
      $search[$k]='`'.preg_quote($search[$k],'`').'`'; 
    } 
  }else{ 
    $search='`'.preg_quote($search,'`').'`'; 
  } 
  //把图片描述去掉 
  $content=preg_replace("/alt=([^ >]+)/is",'',$content); 
  return preg_replace($search,$replace,$content,$limit); 
}

(2) Use substr_replace Function to implement, but here only one replacement can be achieved

//首先找到关键字所在位置,然后使用 substr_replace(系统函数)进行替换操作 
function str_replace_once($search,$replace,$content){ 
  //把图片描述去掉 
  $content=preg_replace("/alt=([^ >]+)/is",'',$content); 
  $pos=strpos($content,$search); 
  if($pos===false){ 
    return $haystack; 
  } 
  return substr_replace($content,$replace,$pos,strlen($search)); 
}

The above is the detailed content of How to replace php keywords. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Peak: How To Revive Players
1 months ago By DDD
PEAK How to Emote
4 weeks ago By Jack chen

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)