简体   繁体   English

php str_replace()用于/和&

[英]php str_replace() for / and &

We have created an xml feed in Magento for some products from different categories. 我们在Magento中为不同类别的某些产品创建了xml feed。 Some products have a name with a &, or the name of the brand is with a /. 某些产品的名称带有&,或者品牌名称带有/。 When we generate the feed, the feed gives an error. 当我们生成提要时,提要给出了一个错误。

xml错误

In previous feeds we've used this line of code: 在以前的提要中,我们使用了以下代码行:

<name><? return str_replace("&", "&amp;", "{name}"); ?></name>

to turn the & into a &amp; &变成&amp; and this worked. 这有效。 Now we need to do the same thing with the / . 现在我们需要做同样的事情用/ We need to turn the / into 'and' or something like that. 我们需要将/变成'and'或类似的东西。

There are 2 (different) lines of codes in our xml feed where we can paste the str_replace: xml提要中有2行(不同)代码,我们可以在其中粘贴str_replace:

<? if ("{name}" != "") return "<li><b>Name:</b> {name}</li>"; ?>

and

<name>{name}</name>

Where should the str_replace be pasted? 将str_replace粘贴到哪里?

I got the xml working. 我得到了XML的工作。

<serie><? return str_replace(array('&', '/') , array('&amp;','and'), "{serie}"); ?></serie> 

did the trick for me. 为我做了把戏。

The {serie} on the end of the code was the problem. 代码末尾的{serie}是问题所在。 I don't understand why it worked in the previous feeds, but u don't hear me complaining haha. 我不明白为什么它在以前的提要中起作用,但是您听不到我抱怨哈哈。

Thanks. 谢谢。

在这种情况下,请勿使用str_replace,为此目的有htmlentities函数: https : //secure.php.net/manual/en/function.htmlentities.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM