Barbacamanitu Posted March 14, 2008 Share Posted March 14, 2008 Hey guys, I am a total noob to this. I have tried learning how to use preg_replace correctly, but I just can't grasp it. I would love it if someone could help me with this problem. I want a piece of code that changes all of the image tags in a page into Adbrite's BritePic code. Eg: <img src="http://www.yoursite.com/yourpic.jpg"> get's changed into: <script> britepic_src="http://www.yoursite.com/yourpic.jpg"; britepic_id="your_unique_britepic_id"; </script> <script src='http://www.britepic.com/britepic.js'> </script> <noscript> <img src="http://www.yoursite.com/yourpic.jpg"> </noscript> If someone could help me out here, that would be absolutely great. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
sasa Posted March 14, 2008 Share Posted March 14, 2008 try <?php $test = ' <img src="http://www.yoursite.com/yourpic.jpg"> bla bla <img src="pic2.bmp" alt="bla"> bla 2 2 bla '; $x = '/(<img.*src="([^"]+)"[^>]*>)/i'; $to = '<script> britepic_src="$2"; britepic_id="your_unique_britepic_id"; </script> <script src=\'http://www.britepic.com/britepic.js\'> </script> <noscript> $1 </noscript>'; echo preg_replace($x,$to,$test); ?> Quote Link to comment Share on other sites More sharing options...
Barbacamanitu Posted March 14, 2008 Author Share Posted March 14, 2008 Thank you for your reply. It almost worked correctly, but the britepics never loaded up. They all just said, "Loading britePic 0%" I think it might be because I am using the Phproxy script, and it is encoding the img urls. Are you familiar with this proxy script? Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 15, 2008 Share Posted March 15, 2008 You might want to refer to the author of the script for that issue... Also, as a minor note, if you're going to use something like this: [^>]*> You're better off using a greedy quantifier [^>]+> Especially in a loop. It causes the regex to check back fewer times. In this instance you could probably even use '++', which doesn't check back at all. Efficiency never hurts. Quote Link to comment Share on other sites More sharing options...
dingus Posted March 17, 2008 Share Posted March 17, 2008 i am also using britepics with my proxy in development (foxypoxy.theatticnetwork.net/demo) and i am having some trouble as the pics need to be submitted to adbrite before they can be used adbrite dose not currently have an API access so i am in the process of writing on using curl another word of advice which is something i am crossing is you will need a shore block of code to set height and width limits on the image (for example only images greater then 200x200) other wise you start putting ads links and stuff never a good thing Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.