Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

str_replace Large string

Posted on 2013-11-19
5
Medium Priority
?
789 Views
Last Modified: 2013-11-24
I have a 10mb file I'm loading into a var and then trying to do str_replace on it, something simple like this

$xml=file_get_contents($_SERVER['DOCUMENT_ROOT']."/products.xml");
$xml=str_replace('Color','Colour',$xml);

There are lots more str_replace, but at the moment none of them are working.
If I cut the file down to 1mb it works ok
Is there a limit to the string size in php? Is there a better way to do this?
0
Comment
Question by:Gary
5 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39660746
Have you tried to use preg_replace? Maybe it works...
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39660747
There is a limit to how much memory PHP is allowed to use.  A 10MB file will take at least 20MB to edit plus PHP still needs room to run.  See how much memory your host is allowing for PHP.
0
 
LVL 58

Accepted Solution

by:
Gary earned 0 total points
ID: 39660835
Found the cause, for some reason the file was getting saved as Little Endian encoding.
Only realised when I looked at the source and I saw some funny characters at the beginning
Only seems to be the case when I save the file first.  In the future the file will be stored directly into memory - and it still works fine after testing a direct load into memory.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39660984
PHP str_replace() is the wrong function to use with UTF-8 data.  There are multi-byte functions.  This article explains some of the issues.  Basically PHP is just stupid about "Endianness" and multi-byte characters.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html
0
 
LVL 58

Author Closing Comment

by:Gary
ID: 39672400
See comment above
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question