検索
ホームページphp教程php手册PHP配列1対1置換実装コード

PHP配列1対1置換実装コード

Jun 13, 2016 am 11:58 AM
headerhtphptextコードコピー成し遂げる配列交換する

コードをコピー コードは次のとおりです。


header("Content-type: text/html; charset=utf -8");
function multiple_replace_words($word,$replace,$string,$tmp_match='#a_a#'){
preg_match_all('/'.$word.'/',$ string,$matches); //すべてのキーワードに一致します
$search =explode('/,/',$matches[0]).'/'); // 一致するキーワードがありません
if(empty($matches[0])) return false
//特別な置換設定
$count = count($matches[0]); foreach( $replace as $key=>$val){
if(!isset($matches[0][$key])) unset($replace[$key]); //境界置換
}
//特別な置換配列と一致する配列をマージします
for($i=0;$i$matches[0][$ i] = isset($replace[$i])? $replace[$i] : $matches[0][$i];
$replace = $matches[0]; /置換ループの防止、つまり置換 文字は置換された文字のままですが、一時的に特定の文字に置換されます $tmp_match
$replace = implode(',',$replace); = str_replace($word,$tmp_match,$replace ); //一致する文字の一時置換
$replace =explode(',',$replace);
//置換処理
$string = preg_replace ($search,$replace,$string,1) ; //一度に配列内の 1 文字だけを置換します
$string = str_replace($tmp_match,$word,$string); //一時的に置換された一致を復元します文字
return $string;
}
//例 1
$string = 'aaabaaacaaadaaa';
$word = 'aaa';
$replace = array(null,') xxx','yyy');
エコー '元のテキスト :'.$string.'
出力: '.multiple_replace_words($word,$replace,$string)'
';
//例 2
$string = '中国語 aaab 中国語 ccaaad 中国語 eee';
$word = '中国語'
$replace = array(null) ,' (中国語 2 を置換)',' (中国語 3 を置換) ');
echo '元のテキスト: '.$string.'
出力: '.multiple_replace_words($word,$replace, $string);
/*
出力結果:
元のテキスト: aaabaaacaaadaaa
出力: aaabxxxcyyydaaa
元のテキスト: 中国語 aaab 中国語 ccaaad 中国語 eee
出力: 中国語 aaab (中国語を置き換えます) 2) ccaaad (中国語を置き換えます 3) eee
//*/


著者: Zjmainstay

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!