Solved

str_replace Large string

Posted on 2013-11-19
5
721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

627 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