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
https://forums.phpfreaks.com/topic/96087-preg_replace-help/
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
https://forums.phpfreaks.com/topic/96087-preg_replace-help/#findComment-491939
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/96087-preg_replace-help/#findComment-492207
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
https://forums.phpfreaks.com/topic/96087-preg_replace-help/#findComment-493000
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
https://forums.phpfreaks.com/topic/96087-preg_replace-help/#findComment-493777
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.