2007年03月03日
文字列データの置換
str_replace()関数を使う。
str_replace()…引数に含まれる文字列を、指定した文字列に置き換える。
str_replace (検索文字, 置換後の文字列, 元の文字列)
☆サンプルデータ
<? php
$beginner = "初心者のためのホームページ作り";
$replace = "ホームページ作り";
$scriptphp = "PHP入門";
$word = str_replace($replace, $scriptphp, $beginner);
print($word);
?>
☆実行結果
⇒初心者のためのPHP入門
-----------------------------------------------------------
今回行ったことは上記を応用して絶対パスを相対パスに変換。
☆サンプルデータ
<? php
$path = /home/example/public_html/example.html;
$ori = $path;
$rep = "/home/example/public_html/";
$scr = "http://www.example.jp/";
$acc = str_replace($rep, $scr, $ori);
print($acc);
?>
☆実行結果
⇒http://www.example.jp/example.html
[参考URL]
http://www.scollabo.com/banban/php/ref/ref_str_replace.html
str_replace()…引数に含まれる文字列を、指定した文字列に置き換える。
str_replace (検索文字, 置換後の文字列, 元の文字列)
☆サンプルデータ
<? php
$beginner = "初心者のためのホームページ作り";
$replace = "ホームページ作り";
$scriptphp = "PHP入門";
$word = str_replace($replace, $scriptphp, $beginner);
print($word);
?>
☆実行結果
⇒初心者のためのPHP入門
-----------------------------------------------------------
今回行ったことは上記を応用して絶対パスを相対パスに変換。
☆サンプルデータ
<? php
$path = /home/example/public_html/example.html;
$ori = $path;
$rep = "/home/example/public_html/";
$scr = "http://www.example.jp/";
$acc = str_replace($rep, $scr, $ori);
print($acc);
?>
☆実行結果
⇒http://www.example.jp/example.html
[参考URL]
http://www.scollabo.com/banban/php/ref/ref_str_replace.html