Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP SimpleXML节点上使用ucfirst()?_Php_Simplexml - Fatal编程技术网

如何在PHP SimpleXML节点上使用ucfirst()?

如何在PHP SimpleXML节点上使用ucfirst()?,php,simplexml,Php,Simplexml,我使用php和simplexml解析url。我想获取simplexml节点的值并对其进行更改,首先将其转换为字符串,但ucfirst()不适用于该字符串 $xml = simplexml_load_file($url); foreach($xml->offers->offer as $offer) { $bodyType = (string) $offer->{"body-type"}; //I convert simplexml to string first

我使用php和simplexml解析url。我想获取simplexml节点的值并对其进行更改,首先将其转换为字符串,但ucfirst()不适用于该字符串

$xml = simplexml_load_file($url); 

foreach($xml->offers->offer as $offer)
{
    $bodyType = (string) $offer->{"body-type"}; //I convert simplexml to string first  
    echo ucfirst($bodyType); // In this line ucfirst doesn't work
}
如何处理

最新消息:问题出在西里尔字母上,因为UCS最初只使用拉丁语。 工作解决方案是使用此功能:

    $bodyType = (string) $offer->{"body-type"};
    $encoding='UTF-8';
    $str = mb_ereg_replace('^[\ ]+', '', $bodyType); 
    $str = mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding). mb_substr($str, 1, mb_strlen($str), $encoding);

亲爱的plz,也请分享您的xml文件数据。我使用了以下方法,效果很好

<?xml version="1.0"?>
<data>
    <offers>
        <offer>
            <body-type>offer 1</body-type>
        </offer>
        <offer>
            <body-type>offer 2</body-type>
        </offer>
    </offers>
</data>

报价1
提议2
我的输出是

Offer 1
Offer 2

HTML: Offer 1<br />Offer 2<br />
Offer 1
提议2
HTML:优惠1
优惠2
通过遵循php代码

<?PHP
$url = "test.xml";
$xml = simplexml_load_file($url); 

foreach($xml->offers->offer as $offer)
{
    $bodyType = (string) $offer->{"body-type"}; //I convert simplexml to string first  
    echo ucfirst($bodyType); // In this line ucfirst doesn't work
    echo '<br />';
}


?>

给定中的
test.xml
,实际上甚至可以省略类型转换。这对我来说也很有效:

<?php
$url = "test.xml";
$xml = simplexml_load_file($url); 

foreach($xml->offers->offer as $offer) {
    echo ucfirst($offer->{"body-type"}) .'<br>';
}
注意您没有为xml提供内容类型/字符集头!我想补充一点。
无论如何,您可能想看看这个:->
iconv(“cp1251”,“UTF-8”,“$str”)


实际上,文件编码是西里尔文windows-1251,这可能是有意义的

为什么??当然,您可以使用有效的UTF-8!下面是使用转换的XML中的一个示例节点(可能看起来很奇怪,但呈现得很完美!)


2013-02-07 02:00:08格林尼治标准时间+4
carsguru.net
http://carsguru.net/used/5131406/view.html
2013-02-07
ГАЗ
2705
2003
Санкт-Петербург
8-921-997-74-06
150000
鲁尔
левый
км
194
2300
в наличии
Хорошее
синий
фургон
бензин
задний
ручная
98
http://carsguru.net/clf/03/af/9c/8b/used.4r9v39h31facog8cs0w0wk8ws.jpg.medium.jpg
http://carsguru.net/clf/ae/51/be/3a/used.bxyc3q9mx80sko0wg80880w0k.jpg.medium.jpg
http://carsguru.net/clf/28/dc/c1/d4/used.8i1b76l1b8o4cwg8gc08oos4s.jpg.medium.jpg
http://carsguru.net/clf/55/3d/37/10/used.7dmn7puczuo0wo4cs8kko0cco.jpg.medium.jpg
http://carsguru.net/clf/49/02/15/54/used.7k8lhomw4j4s4040kssk4kgso.jpg.medium.jpg
Магнитола
Подогрев зеркал

你说的“不起作用”是什么意思<代码>变量转储($bodyType)我临时添加了实际的url和注释。是的,但是你可以看到ucfirst()不适用于这个xml字符串,第一个字母没有大写!不,我看不出来。我不会读/说西里尔语。对我来说,一切都一样但你是对的。ucfirst不仅仅适用于拉丁字母。你在这里试过这个功能吗:?谢谢你的帮助,我在俄罗斯网站上找到了解决方案!我添加了实际的url和临时的注释。
$url = "http://carsguru.net/x/used/exchange/4.xml";
$xml = simplexml_load_file($url); 

header('Content-Type: text/html; charset=utf-8');
foreach($xml->offers->offer as $offer) {
    echo ucfirst($offer->{"body-type"}) .'<br>';
}
фургон
универсал
хэтчбек
хэтчбек
минивэн
минивэн
минивэн
седан
седан
универсал
хэтчбек
универсал
седан
хэтчбек
седан
<?xml version="1.0" encoding="UTF-8"?>
<auto-catalog>
    <creation-date>2013-02-07 02:00:08 GMT+4</creation-date>
    <host>carsguru.net</host>
    <offers>
        <offer type="commercial">
            <url>http://carsguru.net/used/5131406/view.html</url>
            <date>2013-02-07</date>
            <mark>&#1043;&#1040;&#1047;</mark>
            <model>2705</model>
            <year>2003</year>
            <seller-city>&#1057;&#1072;&#1085;&#1082;&#1090;-&#1055;&#1077;&#1090;&#1077;&#1088;&#1073;&#1091;&#1088;&#1075;</seller-city>
            <seller-phone>8-921-997-74-06</seller-phone>
            <price>150000</price>
            <currency-type>RUR</currency-type>
            <steering-wheel>&#1083;&#1077;&#1074;&#1099;&#1081;</steering-wheel>
            <run-metric>&#1082;&#1084;</run-metric>
            <run>194</run>
            <displacement>2300</displacement>
            <stock>&#1074; &#1085;&#1072;&#1083;&#1080;&#1095;&#1080;&#1080;</stock>
            <state>&#1061;&#1086;&#1088;&#1086;&#1096;&#1077;&#1077;</state>
            <color>&#1089;&#1080;&#1085;&#1080;&#1081;</color>
            <body-type>&#1092;&#1091;&#1088;&#1075;&#1086;&#1085;</body-type>
            <engine-type>&#1073;&#1077;&#1085;&#1079;&#1080;&#1085;</engine-type>
            <gear-type>&#1079;&#1072;&#1076;&#1085;&#1080;&#1081;</gear-type>
            <transmission>&#1088;&#1091;&#1095;&#1085;&#1072;&#1103;</transmission>
            <horse-power>98</horse-power>
            <image>http://carsguru.net/clf/03/af/9c/8b/used.4r9v39h31facog8cs0w0wk8ws.jpg.medium.jpg</image>
            <image>http://carsguru.net/clf/ae/51/be/3a/used.bxyc3q9mx80sko0wg80880w0k.jpg.medium.jpg</image>
            <image>http://carsguru.net/clf/28/dc/c1/d4/used.8i1b76l1b8o4cwg8gc08oos4s.jpg.medium.jpg</image>
            <image>http://carsguru.net/clf/55/3d/37/10/used.7dmn7puczuo0wo4cs8kko0cco.jpg.medium.jpg</image>
            <image>http://carsguru.net/clf/49/02/15/54/used.7k8lhomw4j4s4040kssk4kgso.jpg.medium.jpg</image>
            <equipment>&#1052;&#1072;&#1075;&#1085;&#1080;&#1090;&#1086;&#1083;&#1072;</equipment>
            <equipment>&#1055;&#1086;&#1076;&#1086;&#1075;&#1088;&#1077;&#1074; &#1079;&#1077;&#1088;&#1082;&#1072;&#1083;</equipment>
        </offer>
    </offers>
</auto-catalog>