Solved

str_replace Large string

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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