the_opposite Posted February 9, 2011 Share Posted February 9, 2011 Hi all, I am at a loss to understand why this doesn't work. I have a simple function: function toHTML($inputText) { $inputText = str_replace('<','<',$inputText); $inputText = str_replace('>','>',$inputText); $inputText = str_replace('&','&',$inputText); $inputText = str_replace("'",''',$inputText); $inputText = str_replace('"','"',$inputText); return $inputText; } When I run code through it, specifically xml files from which I need to remove/replace the above charachters, it seems to refuse to replace all of the characters. For example, when I run the following text through this function, the '&' charachter I have highlighted is not replaced: <?xml version="1.0" ?> <!-- Purchase Order XML Copyright 2010 Version 1.0 --><PurchaseOrder> <Envelope> <SenderID>xxx</SenderID> <ReceiverID>xxx</ReceiverID> <DateStamp type="MessageDate">2011-02-09</DateStamp> <VersionID>1.4</VersionID> </Envelope> <Header> <Party type="SoldTo"> <IDNumber type="Supplier">xxx</IDNumber> <IDNumber type="Reseller">xxx</IDNumber> <Name>John Doe</Name> <AddressLine1>Temp</AddressLine1> <AddressLine2>Address 1</AddressLine2> <AddressLine3>Address 1</AddressLine3> <Street>Address 1</Street> <City>West Midlands</City> <PostCode>DY9 8QH</PostCode> <CountryCode>GB</CountryCode> <ContactPhone>01384444555</ContactPhone> </Party> <Party type="ShipTo"> <IDNumber type="Supplier">xxx</IDNumber> <IDNumber type="Reseller">DIRECTDROP</IDNumber> <Name> </Name><AddressLine1></AddressLine1> <Street></Street> <City></City><PostCode></PostCode> <CountryCode></CountryCode> <ContactPhone></ContactPhone> </Party> <!-- Kill/Fill 1-Back order allow. 2-Back order allow but ship complete. 3-Line item kill or fill. 4-whole order kill or fill. --> <KillOrFill>1</KillOrFill> <TotalValues> <Currency>GBP</Currency> <VATRate>20.0</VATRate> <MonetaryAmount type="FreightCharge">5.00</MonetaryAmount> <MonetaryAmount type="VATAmount">1</MonetaryAmount> <MonetaryAmount type="GoodsNetTotal">0</MonetaryAmount> <MonetaryAmount type="OrderNetTotal">5</MonetaryAmount> <MonetaryAmount type="GrossTotal">6</MonetaryAmount> </TotalValues> <OrderType>Direct Drop</OrderType> <DocumentReference> <DocumentNumber type="ResellerPO">J653</DocumentNumber> <DateStamp type="ResellerPO">2011-02-09</DateStamp> </DocumentReference> </Header> <Line> <Item> [b][color=red]<ProductName>Freecom Hard Drive Dock 2.5/3.5 USB&SATA</ProductName>[/color][/b] <ProductNumber type="Supplier">3294</ProductNumber> <Quantity type="Ordered">2</Quantity> <MonetaryAmount type="UnitPrice"></MonetaryAmount> <MonetaryAmount type="LineTotal">0</MonetaryAmount> </Item> </Line> </PurchaseOrder> The '&' between USB&SATA is not replaced with & on this line: <ProductName>Freecom Hard Drive Dock 2.5/3.5 USB&SATA</ProductName> Does anybody know why? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/227142-str_replace-for-single-charachter-not-working/ Share on other sites More sharing options...
Roman Fedorov Posted February 9, 2011 Share Posted February 9, 2011 Hi, First of all your code is wrong because first you replace <, > chars with "&....;" and then you replace the "&" char, so it will replace also & that you put for < and > chars. And anyway i think that this function is what you are looking for: http://php.net/manual/en/function.htmlentities.php Link to comment https://forums.phpfreaks.com/topic/227142-str_replace-for-single-charachter-not-working/#findComment-1171734 Share on other sites More sharing options...
the_opposite Posted February 9, 2011 Author Share Posted February 9, 2011 Thank you Roman, I wasn't aware of that htmlentities function Link to comment https://forums.phpfreaks.com/topic/227142-str_replace-for-single-charachter-not-working/#findComment-1171745 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.