SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP regular expressions and replace help

    I've got a bunch of entries in a database like this

    Code:
    
    <object><embed src="http://www.flashgamesnexus.com/gamefiles/gemcraft/gemcraft.swf" width="640" height="480"></object></center>
    I want to replace them all so it looks more like this:

    Code:
    <center>
    <object><embed src="http://www.flashgamesnexus.com/gamefiles/gemcraft/gemcraft.swf" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" / width="640" height="480"></object></center>
    Its basically the same on every entry. Just need to insert some text in the middle. How would I do this in PHP and regular expressions?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would simply do a string_replace if it doesn't require any complicated logic.

    str_replace('<embed', '<embed type="application/x-shockwave-flash" and_anything_else_you_want_added ', $string)

    Make sure there is a space after what's added, like I did.

    Also although this might be off-topic, you really shouldn't store HTML like that, because you run into maintenance issues like this. HTML should be generated as part of the display. The only real exception is if it takes heavy processing power and you need to cache the HTML, in which case you store the raw data (so it's easy to regenerate the HTML) with the cached (so it's quicker to output).

  3. #3
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I know, its not very convenient on these update issues but its also a lot more flexible to store HTML at times rather than generate.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •