Solved

str_replace Large string

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

Expert Comment

by:Marco Gasi
Comment Utility
Have you tried to use preg_replace? Maybe it works...
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
See comment above
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now