#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Posts
    17
    Rep Power
    0

    PHP GREP - single to double quote in tags


    Hi,

    I have a string like:

    Code:
    <p class='essay_caption'>This is Bob's test</p>
    I want to change the string so all the ' become " inside the tag, but so it does not affect the ' in Bob's:

    Code:
    <p class="essay_caption">This is Bob's test</p>
    I'm using PHP 5 and wonder if this can be done with ereg_replace.

    Thanks for any help

    PG
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    51
    Rep Power
    11
    This will replace the ' in Bob's,
    Code:
    ([a-zA-Z])'([a-zA-Z])
    Now that I got you started, i'll leave you to do the reverse.
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397
    So all you really want to do is replace all the apostrophes in HTML tags with quotes?
    PHP Code:
    $text "<p class='essay_caption'>This is Bob's test</p>";

    echo 
    preg_replace('/<([^<>]+)>/e''"<" . str_replace("\\\\\'", \'"\', "$1") . ">"'$text); 
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Posts
    17
    Rep Power
    0
    requinix - thank you!

    That is exactly what I needed.

IMN logo majestic logo threadwatch logo seochat tools logo