• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

PHP string replace

Hello experts!

I want to use php to convert the below string from Before to After. The problem is that the below string is not always the same information. However, the information will always come in the same format. Is there an easy way with str_replace to do this?

Photo name: ___________________ Open House Jane White BOONE county zionsville 1428 DOMINION DR 2-5 $184,900 3 Bedrooms / 2 Full,  Half Baths 846-7751 (Condominium) - 21106026 Special Financing:  

4/17/2011,2-5,21106026, Jane White
4 Solutions
It would require pretty advanced parsing, which wouldn't be possible with str_replace, only with a large amount of Regular Expressions. If data is always in same format and same data, then you should read more about RegExpr here: http://us.php.net/regex
Lukasz ChmielewskiCommented:
Can you see any pattern in that ? Anything that repeats is the way to go.
Dave BaldwinFixer of ProblemsCommented:
It looks like '21106026' is an id number.  If it is, you could search for that and just replace the whole string with the new info.
Ray PaseurCommented:
This has the look of something that should arrive in an XML string so you can parse it easily.

Consider the programming issue if Jane White got married to someone named Boone and used the name Jane White Boone.  All the fields would be offset and the parser would probably break.  Or what if Madonna started selling houses.  No last name, eh?  Those kinds of things need to be thought out.

The string 2-5 could potentially be identified by a single-digit number, followed by a dash, followed by a single-digit number, with the whole of it wrapped in blanks.

The date 4/17/2011 can be gotten from using the PHP date() function.  Use a pattern like 'n/j/Y' to get the string.

The 21106026 could be identified by an 8-character numeric string.
spogglesAuthor Commented:
Thank you experts. This gave me exactly what I needed to parse the file.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now