search & replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

search & replace

Post by Vegan »

can php do basic text editing with search and replace in a file loaded into an array or something like that?
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: search & replace

Post by requinix »

Yup.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: search & replace

Post by Celauran »

Can you be a little more specific? What is it you're trying to do?
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: search & replace

Post by Vegan »

I was looking at cleaning up POST forms before feeding them to a database
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: search & replace

Post by Christopher »

Use str_replace() for simple replacements. And preg_replace() for more complex matching. There are functions like trim() for additional cleanup. And, of course, escape the data with the database's escaping method before writing to the database.

There are also special-case cleanups. For example, str_replace(array("\n", "\r", "\t"), '', $data) for saving data that may later be exported to a tab delimited file (e.g. SELECT OUTFILE).
(#10850)
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: search & replace

Post by Vegan »

Excel is hard pressed with tab delimited so I tend to prefer another type of delimiter
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply