Jump to content

Preg_replace help


Barbacamanitu

Recommended Posts

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. :D

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.