I use php and simplexml for parse url. I want take value of simplexml node and change it, first I convert it to string, but ucfirst() doesn't work for that string.
$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
}
How to deal with it?
UPDATE: Problem was in Cyrillic letters, since ucfirst works only with Latin. Working solution is to use this function:
$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);
Dear plz share your xml file data also. I have used the following and it is working fine..
<?xml version="1.0"?>
<data>
<offers>
<offer>
<body-type>offer 1</body-type>
</offer>
<offer>
<body-type>offer 2</body-type>
</offer>
</offers>
</data>
my output is
Offer 1
Offer 2
HTML: Offer 1<br />Offer 2<br />
by following php code..
<?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 />';
}
?>
Given the test.xml
from Farrukh's answer , you can actually even omit the typecasting. This works as well for me:
<?php
$url = "test.xml";
$xml = simplexml_load_file($url);
foreach($xml->offers->offer as $offer) {
echo ucfirst($offer->{"body-type"}) .'<br>';
}
Here's a live demo: http://codepad.viper-7.com/L4VwPL
You'll most likely have an encoding issue. When I set the UTF-8 charset explicitly, it works as expected (otherwise simplexml returns corrupted strings only).
$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>'; }
When I run the above snippet, I get this output (stripped):
фургон универсал хэтчбек хэтчбек минивэн минивэн минивэн седан седан универсал хэтчбек универсал седан хэтчбек седан
NOTE You don't serve a content-type/charset header for the xml! I'd add that.
iconv("cp1251", "UTF-8", $str);
Actually file encoding is Cyrillic windows-1251, which is probably make sence.
Why? You can, of course, use valid UTF-8! Here is an example node from your XML converted with this cp1251-to-utf8-function (might look odd, but renders perfectly!)
<?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>ГАЗ</mark> <model>2705</model> <year>2003</year> <seller-city>Санкт-Петербург</seller-city> <seller-phone>8-921-997-74-06</seller-phone> <price>150000</price> <currency-type>RUR</currency-type> <steering-wheel>левый</steering-wheel> <run-metric>км</run-metric> <run>194</run> <displacement>2300</displacement> <stock>в наличии</stock> <state>Хорошее</state> <color>синий</color> <body-type>фургон</body-type> <engine-type>бензин</engine-type> <gear-type>задний</gear-type> <transmission>ручная</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>Магнитола</equipment> <equipment>Подогрев зеркал</equipment> </offer> </offers> </auto-catalog>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.