php字符串替换问题——初学者
要读取的xml文件<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee>
<name>123</name>
<position age="45"> http://www.
</employee>
<employee>
<name>456 </name>
<position age="45"> http://www.
</employee>
<employee>
</employees>
php代码
$string = "123456";
$doc = new DOMDocument();
$doc->load( 'newsgj.xml' );
$employees = $doc->getElementsByTagName( "employee" );
foreach( $employees as $employee )
{
$names = $employee->getElementsByTagName( "name" );
$name = $names->item(0)->nodeValue;
$positions = $employee->getElementsByTagName( "position" );
$position = $positions->item(0)->nodeValue;
$string = str_replace ( $name, "<a href = '$position'>$name</a>", $string );
}
echo iconv ( "GB2312", "UTF-8", $string );
想要的结果是:123456--><a href = " http://www. href=" http://www.,但循环替换时只能把第一个替换(现在的结果:--><a href = " http://www.),还请高手帮帮忙啊!!!