文字列を置換するereg_replace()関数(正規表現対応)

ereg_replace("検索用の正規表現パターン","置換用の正規表現パターン","文字列")

また、Perl互換の構文が使えるereg_replace()関数もある(詳細不明)。

Hello World!
●Hello World!
Hello World!

source

<?php
function myRegTest1($text)	{
	//引数での文字列先頭に●の付いた文字を返す
	$text = ereg_replace ("^", "●", $text);
	return $text;
}

function myRegTest2($text)	{
	//引数の文字列先頭に<u>、末尾に</u>を入れた文字を返す
	$text = ereg_replace ("^", "<u>", $text);
	$text = ereg_replace ("$", "</u>", $text);
	return $text;
}

$tmpStr1 = "Hello World!";
echo $tmpStr1."<br>";
echo myRegTest1($tmpStr1)."<br>";
echo myRegTest2($tmpStr1)."<br>";
?>