PHP string replace

Posted on 2011-04-22
Last Modified: 2012-05-11
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
Question by:spoggles
    LVL 4

    Assisted Solution

    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:
    LVL 27

    Assisted Solution

    by:Lukasz Chmielewski
    Can you see any pattern in that ? Anything that repeats is the way to go.
    LVL 82

    Assisted Solution

    by:Dave Baldwin
    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.
    LVL 107

    Accepted Solution

    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.

    Author Comment

    Thank you experts. This gave me exactly what I needed to parse the file.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    755 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

    22 Experts available now in Live!

    Get 1:1 Help Now