文字列を置換する02
☆★データーでの確認★☆
指定位置の文字列を置換する
substr_replace関数
- 指定した位置から指定長さ分の別の文字に置き換える。
引数は「もとの文字列」「置き換える文字列」「開始位置」「長さ」の順で指定。先頭の文字位置=0、全角文字の長さ=2として計算。 - 第4引数の「長さ」と第2引数の「置き換える文字列」の長さによっては、文字列の全体の長さが変わる場合もある。
【 書式 】
<?php $data = "PHP:Hypertext Preprocessor"; // 1文字目から2文字を"**"に置き換える print substr_replace($data, "**", 0, 2) . "<br>"; // 5文字目から2文字を"**"に置き換える print substr_replace($data, "**", 4, 2) . "<br>"; // 15文字目から4文字を"****"に置き換える print substr_replace($data, "*****", 14, 4) . "<br>"; // 15文字目から4文字を"**"(2文字分)に置き換える // 17文字目と18文字目は削除される print substr_replace($data, "**", 14, 4) . "<br>"; // 最初の「P」だけを探して"*"に置き換える print substr_replace($data, "*", strpos($data, "P"), 1) . "<br>"; // 最後の「p」だけを探して"*"に置き換える print substr_replace($data, "*", strrpos($data, "P"), 1) . "<br>"; ?>
ソースコード
ソースコード【 HTML 】
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <meta name="description" content="文字列の処理"> <meta name="keywords" content="PHP,数値の処理"> <meta name="viewport" content="width=device-width"> <title>文字列の処理</title> <!-- IE対策 --> <!--[if ite IE 9]> <script src="http://html5shiv.googlecode.com/svn/ttunk/html5.js"></script> <![endif]--> <!--[if lte IE 9]> <script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script> <![endif]--> <style> article,aside,dialog,figure,footer,header, hgroup,menu,nav,section{display: block;} </style> <link href="css/style.css" rel="stylesheet" type="text/css" media="all"> </head> <body> <div id="container"> <header> <h1>文字列を置換する</h1> </header> <div class="wrapper"> <section> <h1 class="title_top">指定位置の文字列を置換する</h1> <p class="point"><span class="p">P</span>oint</p> <h2 class="title_main">substr_replace関数<span class="title_main_txt"> </span></h2> <dl> <dt>substr_replace関数</dt> <dd> <ul> <li>指定した位置から指定長さ分の別の文字に置き換える。<br>引数は「もとの文字列」「置き換える文字列」「開始位置」「長さ」の順で指定。先頭の文字位置=0、全角文字の長さ=2として計算。</li> <li>第4引数の「長さ」と第2引数の「置き換える文字列」の長さによっては、文字列の全体の長さが変わる場合もある。</li> </ul> </dd> </dl> <h2 class="txt_web">< ブラウザでの表示 ></h2> <div class="txt_php"> <?php $data = "PHP:Hypertext Preprocessor"; // 1文字目から2文字を"**"に置き換える print substr_replace($data, "**", 0, 2) . "<br>"; // 5文字目から2文字を"**"に置き換える print substr_replace($data, "**", 4, 2) . "<br>"; // 15文字目から4文字を"****"に置き換える print substr_replace($data, "*****", 14, 4) . "<br>"; // 15文字目から4文字を"**"(2文字分)に置き換える // 17文字目と18文字目は削除される print substr_replace($data, "**", 14, 4) . "<br>"; // 最初の「P」だけを探して"*"に置き換える print substr_replace($data, "*", strpos($data, "P"), 1) . "<br>"; // 最後の「p」だけを探して"*"に置き換える print substr_replace($data, "*", strrpos($data, "P"), 1) . "<br>"; ?> </div> </section> <!-- /.wrapper --> </div> <footer> <h1>PHPスクリプトの基本</h1> <small>Copyright© 2013 webry All Rights Reserved.</small> <small>参考資料:<span class="txt_small"><a href="d.hatena.ne.jp/webry/20130825/p2#php_sample_site" target="_blank">速攻&活用! Web開発者のためのPHPベストサンプル集</a></span> </footer> <!-- /#container --> </div> </body> </html>
@charset "utf-8"; /* resetcss ------------------------------------------------------ */ html,body,div,h1,h2,h3,h4,h5,h6,p,blockquote,pre,address,ul,ol,li,dl,dt,dd,table,th,td,form,fieldset { margin: 0; padding: 0; line-height: 1.5; font-family: "Hiragino Kaku Gothic ProN", Meiryo, sans-serif; } a { text-decoration: none; } ul, ol { list-style-type: none; } img { border: 0; vertical-align: bottom; } /* font ---------------------------------------------------- */ .f_weight {font-weight: bold;} h2 span.title_main_txt { font-size: 86%} footer h1,p.txt_kome {font-size: 13px;} table caption,footer small .txt_small {font-size: 14px;} h2.txt_web {font-size: 16px} /* link ----------------------------------------------------- */ .txt_small a:link {color:#FFA4E9;} .txt_small a:visited,.txt_small a:hover {color:#00AEAE;} .txt_small a:active {color:#00CC14;} /* base ---------------------------------------------------- */ #container { width: 960px; margin: 0 auto; color: #333;} /* layout ---------------------------------------------------- */ header { text-align: center; padding: 24px 50px 18px; background-color: #36F; border-radius: 18px / 18px; color: #fff;} header h1 { border-bottom: 5px dotted #fff;} .wrapper { width: 91%; margin: 0 auto;} h1.title_top { margin: 20px 0 12px; padding: 3px 0 0 5px; border-top: 1px solid #06F; border-right: 1px solid #06F; border-bottom: 1px solid #06F; border-left: 18px solid #06F;} h2.title_main { margin: 0 0 6px 30px; padding: 0 0 3px 70px; font-weight: bold; border-bottom: 1px solid #aaa; color:#F00;} h2 span.title_main_txt { color: #333;} .point { line-height: 1.0; font-family: 'Maven Pro', Helverica, Arial, sans-serif; float: left; background-color: #F60; padding: 12px 26px; border-radius: 55px / 48px; text-align: center; font-weight: bold; font-size: 14px; color: #fff;} .point .p { display: block; font-size: 32px; line-height: 1.0;} dl dt { margin: 0 0 0 88px; font-weight: bold;} dl dd { margin: 0 0 18px 128px;} /* .table */ table { width: auto; margin: 35px auto 0; border: 2px #001693 solid; border-collapse: collapse;} table th,table td { border: 1px #001693 solid; padding: 5px 16px 3px;} table th { background-color: #BFCBFF;} td.title_td { text-align: center; font-weight: bold; background: #DBE2FF; color: #FF2F2F;} /* .txt_php */ h2.txt_web { margin: 30px 0 0 0; color: #983A48;} .txt_php { margin: 0 0 10px 0; padding: 20px 15px; border: 2px dotted #3FF; background: #ECF9FF;} div.txt_kome { padding: 6px 12px; border: 2px dotted #C33; border-radius: 12px / 12px; } p span.txt_mark { float: left; font-weight: bold; color: #F33;} p span.txt_kome { width: 97%; float: left; margin: 0 0 0 5px;} p span.txt_mark_mid { display: block; float: left; margin: 0 0 0 55px; font-weight: bold; color: #F33;} p span.txt_kome_mid { display: block; margin: 0 0 0 75px;} dl dd ul li { background: url(../../images/circle.gif) no-repeat left 0.3em; padding-left: 20px;} .inner { padding: 15px 0 0; border-top: 1px dashed #A34F5C; border-bottom: 1px dashed #A34F5C; } .inner dl dt { background: url(../../images/circle02.gif) no-repeat 10px 0.45em; padding: 0 0 0 25px; color: #313875;} /* footer ------------------------------------------------ */ footer { text-align: center; margin-top: 45px; padding: 5px 0; background-color: #36F; border-radius: 6px / 6px; color: #fff; overflow: hidden;} footer small { display: block;} footer small + small { padding: 5px 0;} /* clearFix --------------------------------------------- */ .clearfix:after { content: ""; display: block; clear: both;}
ブラウザで表示【 FireFox 】
IEtester【 IE7 】
IEtester【 IE8 】
IEtester【 IE9 】
ブラウザで表示【 Chrome 】