从输出中去除空白行
我有为 Google Shopping 创建 XML 文档的代码。谷歌被一个空行挂断了。我不知道如何去掉这些线。该代码是从 Magento Commerce 数据库中提取的。
<?php echo '<?xml version="1.0" ?>'; ?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0" xmlns:c="http://www.base.google.com/cns/1.0">
<channel>
<title>Spray Foam Systems</title>
<link>http://www.sprayfoamsys.com/store/</link>
<description>Spray Foam Rigs, Spray Foam Equipment, Sprayfoam Parts and Supplies.</description>
<?php
$con = mysql_connect(REMOVED) or die(mysql_error());
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sprayfoa_store", $con);
$query = mysql_query("SELECT * FROM `catalog_product_flat_1` WHERE `visibility` = 4 ORDER BY entity_id ASC")
or die(mysql_error());
?>
<?php
while($row = mysql_fetch_array($query))
{
?>
<item>
<g:id><?php echo $row['entity_id']; ?></g:id>
<title><?php echo $row['name']; ?></title>
<description><?php echo $row['short_description']; ?></description>
<g:google_product_category>Business & Industrial > Construction</g:google_product_category>
<g:product_type>Spray Foam Parts & Supplies > Fusion AP Parts</g:product_type>
<link>http://sprayfoamsys.com/store/<?php echo $row['url_path']; ?></link>
<g:image_link>http://sprayfoamsys.com/store/media/catalog/product<?php echo $row['small_image']; ?></g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price><?php echo $row['price']; ?></g:price>
<g:brand>
<?php
$entity_id = $row['entity_id'];
$query2 = mysql_query("SELECT * FROM `catalog_product_entity_varchar` WHERE entity_id = '$entity_id' AND attribute_id = '127'")
or die(mysql_error());
while($row2 = mysql_fetch_array($query2))
{
echo $row2['value'];
} ?>
</g:brand>
<g:mpn><?php echo $row['sku']; ?></g:mpn>
</item>
<?php
}
mysql_close($con);
?>
</channel>
</rss>
输出如下:
<item>
<g:id>15</g:id>
<title>DK 817 - Gun Cleaning Solvent and Lubricator - Aerosol Can 16 oz. </title>
<description>DK 817 is a cleaner-lubricant used on for cleaning polyurethane dispensing equipment and guns. DK817 comes in a 16 oz. aerosol can. Contractors praise it's ability to quickly clean components with uncured polyurethane materials. </description>
<g:google_product_category>Business & Industrial > Construction</g:google_product_category>
<g:product_type>Spray Foam Parts & Supplies > Fusion AP Parts</g:product_type>
<link>http://sprayfoamsys.com/store/dk-817-gun-cleaning-solvent-and-lubricator-aerosol-can.html</link>
<g:image_link>http://sprayfoamsys.com/store/media/catalog/product/d/k/dk817.jpg</g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>24.9900</g:price>
<g:brand>brand
</g:brand>
<g:mpn>partnumber</g:mpn>
</item>
<item>
<g:id>16</g:id>
<title>Allegro Full Mask SAR Replacement Lens 9901-09L</title>
<description>Replacement lens for the 9901 Series Allegro Full Face Mask. Clear, scratch resistant lens. Easy to replace. Lens only.
Allegro 9901-09L</description>
<g:google_product_category>Business & Industrial > Construction</g:google_product_category>
<g:product_type>Spray Foam Parts & Supplies > Fusion AP Parts</g:product_type>
<link>http://sprayfoamsys.com/store/allegro-full-mask-sar-replacement-lens-9901-09r.html</link>
<g:image_link>http://sprayfoamsys.com/store/media/catalog/product/a/l/al-9901-09l-1.jpg</g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>75.0000</g:price>
<g:brand>
Allegro</g:brand>
<g:mpn>9901-09L</g:mpn>
</item>
第一项通过没有问题,因为描述标记没有空行,但是第二项给我一个 XML 格式错误,我唯一能想到的是描述标记中的空行
I have code which is creating a XML document for Google Shopping. Google is getting hung up on a blank line. I do not know how to strip the lines out. The code is pulling from a Magento Commerce database.
<?php echo '<?xml version="1.0" ?>'; ?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0" xmlns:c="http://www.base.google.com/cns/1.0">
<channel>
<title>Spray Foam Systems</title>
<link>http://www.sprayfoamsys.com/store/</link>
<description>Spray Foam Rigs, Spray Foam Equipment, Sprayfoam Parts and Supplies.</description>
<?php
$con = mysql_connect(REMOVED) or die(mysql_error());
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sprayfoa_store", $con);
$query = mysql_query("SELECT * FROM `catalog_product_flat_1` WHERE `visibility` = 4 ORDER BY entity_id ASC")
or die(mysql_error());
?>
<?php
while($row = mysql_fetch_array($query))
{
?>
<item>
<g:id><?php echo $row['entity_id']; ?></g:id>
<title><?php echo $row['name']; ?></title>
<description><?php echo $row['short_description']; ?></description>
<g:google_product_category>Business & Industrial > Construction</g:google_product_category>
<g:product_type>Spray Foam Parts & Supplies > Fusion AP Parts</g:product_type>
<link>http://sprayfoamsys.com/store/<?php echo $row['url_path']; ?></link>
<g:image_link>http://sprayfoamsys.com/store/media/catalog/product<?php echo $row['small_image']; ?></g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price><?php echo $row['price']; ?></g:price>
<g:brand>
<?php
$entity_id = $row['entity_id'];
$query2 = mysql_query("SELECT * FROM `catalog_product_entity_varchar` WHERE entity_id = '$entity_id' AND attribute_id = '127'")
or die(mysql_error());
while($row2 = mysql_fetch_array($query2))
{
echo $row2['value'];
} ?>
</g:brand>
<g:mpn><?php echo $row['sku']; ?></g:mpn>
</item>
<?php
}
mysql_close($con);
?>
</channel>
</rss>
The output looks like:
<item>
<g:id>15</g:id>
<title>DK 817 - Gun Cleaning Solvent and Lubricator - Aerosol Can 16 oz. </title>
<description>DK 817 is a cleaner-lubricant used on for cleaning polyurethane dispensing equipment and guns. DK817 comes in a 16 oz. aerosol can. Contractors praise it's ability to quickly clean components with uncured polyurethane materials. </description>
<g:google_product_category>Business & Industrial > Construction</g:google_product_category>
<g:product_type>Spray Foam Parts & Supplies > Fusion AP Parts</g:product_type>
<link>http://sprayfoamsys.com/store/dk-817-gun-cleaning-solvent-and-lubricator-aerosol-can.html</link>
<g:image_link>http://sprayfoamsys.com/store/media/catalog/product/d/k/dk817.jpg</g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>24.9900</g:price>
<g:brand>brand
</g:brand>
<g:mpn>partnumber</g:mpn>
</item>
<item>
<g:id>16</g:id>
<title>Allegro Full Mask SAR Replacement Lens 9901-09L</title>
<description>Replacement lens for the 9901 Series Allegro Full Face Mask. Clear, scratch resistant lens. Easy to replace. Lens only.
Allegro 9901-09L</description>
<g:google_product_category>Business & Industrial > Construction</g:google_product_category>
<g:product_type>Spray Foam Parts & Supplies > Fusion AP Parts</g:product_type>
<link>http://sprayfoamsys.com/store/allegro-full-mask-sar-replacement-lens-9901-09r.html</link>
<g:image_link>http://sprayfoamsys.com/store/media/catalog/product/a/l/al-9901-09l-1.jpg</g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>75.0000</g:price>
<g:brand>
Allegro</g:brand>
<g:mpn>9901-09L</g:mpn>
</item>
The first item passes through no problem as the description tag has no blank lines, however the second item, give me a XML formatting error, the only thing I can think of is the blank line in the description tag
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试像这样删除换行符
请参阅 str_replace()函数。
You could try stripping out the newlines like this
See the str_replace() function in the PHP manual.
trim()
函数将从字符串的开头和结尾删除空格。如果您想删除字符串中任何位置出现的换行符,您可以执行以下操作:str_replace(array("\n","\r\n"), "", $row['short_description'];);
如果您可以控制数据在数据库中的存储方式,我会考虑使用类似的函数“清理”输入,这样您就不必弄乱此页面。
The
trim()
function will remove whitespace from the beginning and end of a string. If you want to remove line-breaks from occurring anywhere in the string, you can do something like this:str_replace(array("\n","\r\n"), "", $row['short_description'];);
If you have any control over how the data is stored in the database, I would consider "cleaning" the input with a similar function so you don't have to clutter up this page.
你可以把它改成这样:
You can change it to something like this:
您可以尝试这个功能
将其放在任何 php 页面的顶部,它会解决您的问题
You can try this function
Put this on top of any php page and it'll take care of your problem