• I don’t understand regex syntax. Could someone assist?

    I would like to add to image URLs a timthumb.php reference. Below is the code which currently merely removes all <img> references:

    <?php ob_start();
       the_content('Read the full post',true);
       $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
       ob_end_clean();
       echo $postOutput;
     ?>

    I would like this line $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents()); to add the following text to the img URL (in bold):

    <img src=”https://MYBLOG.com/timthumb.php?src=IMAGEURLHERE&w=476&h=275&zc=1&q=200“/>

    Basically, I can’t sort out what the syntax is doing, though anyone with PHP experience would be able to. I only need to add text before and after the img URL. Ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter sklundy

    (@sklundy)

    Resolved. To make timthumb apply to the entirety of a post, use the following code:

    <?php ob_start();
       the_content('Read the full post',true);
    
    $postOutput= preg_replace('#<img(.*?)src="([^"]*/)?(([^"/]*)\.[^"]*)"([^>]*?)>#', '<a href=$2$3><img$1src="https://....timthumb.php?src=$2$3&w=476&h=275&zc=1&q=200"></a>', ob_get_contents());  
    
       ob_end_clean();
       echo $postOutput;
     ?>

    superb; it works ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Preg_replace() regex syntax to add timthumb to img URL?’ is closed to new replies.