Solved

str_replace Large string

Posted on 2013-11-19
5
639 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 83

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now