Replace HTML tag wth content depending on attribute

Discussion in 'PHP' started by Python, Jun 25, 2011.

  1. #1
    Hi,
    Lets say I have an array of names like this...

    $names['john'] = 'John Fisher';
    $names['frank'] = 'Frank Jones';
    $names['dave'] = 'Dave Williams';
    $names['adam'] = 'Adam King';
    PHP:
    I then had a string, like this...

    $string = 'Johns full name is <tag name="john"/>.<br/>
    Franks full name is <tag name="frank"/>';
    PHP:
    That should result in this...

    I basically want to replace the <tag> element with the persons full name. It is the 'name' attribute that specifies which name to use from the array. I know str_replace will do this, but I can't figure out how.

    Thanks!
     
    Python, Jun 25, 2011 IP
  2. Python

    Python Well-Known Member

    Messages:
    680
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    120
    #2
    I figured it out! Thanks!
     
    Python, Jun 25, 2011 IP
  3. prashishh

    prashishh Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    please post how you did it :)
     
    prashishh, Jun 25, 2011 IP
  4. AJstyle

    AJstyle Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    please explain how you did??
     
    AJstyle, Jun 25, 2011 IP
  5. Python

    Python Well-Known Member

    Messages:
    680
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    120
    #5
    $output = preg_replace("#\<tag.+name\=[\"|\'](.+)[\"|\'].*\>.*\<\/tag\>#U","{tag_$1}",$output);
    PHP:
    I then looped through the array of names are replaced {tag_x} using str_replace from the array.
     
    Python, Jun 26, 2011 IP
  6. AJstyle

    AJstyle Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    thx for the reply...
     
    AJstyle, Jun 26, 2011 IP