Jump to content


Small Regex Help


3 replies to this topic

#1 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 20 August 2006 - 01:22 AM

Heh, I seem to be awesome at regex in a .htaccess file when using rewrite rules, but then when I get back to my PHP scripts, I can't figure a single thing out. :D

Here's what I want to locate and replace. Any instance of this.
<img src="/folder/image.jpg" width="100" height="100">
And I want to whipe out the height/widths, XHTML validate it, and transform it for a thumbnail script I made for this.

<a href="/folder/image.jpg"><img src="/resize.php?src=folder/image.jpg" alt="" /></a>
Basically, it takes the image's source, takes out the slash (which may or not be there), pushes in the '/resize.php?src=' then whipes the height/width and adds a blank alt attribute and a backslash for self-closing. It then takes the image and wraps it in a hyperlink with the image's source.

This is for a content management system I've built for someone by the way, and he has a habit of using Dreamweaver for writing his tutorials, and I need to automatically make all the images into thumbnails and clean up other code before I put them into the database.
Any chance anyone could help me out a bit. :P

#2 magiceye

    Young Padawan

  • Members
  • Pip
  • 11 posts

Posted 20 August 2006 - 06:12 AM

<?php 
$str = preg_replace("/<img (.*?) src=([\"\'])(.*?)([\"\']) (.*?)>/i", "<a href=\"$3\"><img src=\"/resize.php?src=$3\" alt=\"\" /></a>", "This is <img 
width=\"230\" src=\"blah.png\" height=\"60\">sdfsdf");
print $str;
?>


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 20 August 2006 - 10:52 AM

That doesn't handle the optional "/".
$str = preg_replace('#<img src=([\"\'])(/|)(.*?)(\\1)(.*?)>#i', '<a href="/\\3"><img src="/resize.php?src=\\3" alt="" /></a>', 'This is <img 
width="230" src="blah.png" height="60">sdfsdf');
Haven't tested, but that should work (hopefully). And since dreamweaver likes to keep things in a certain order, i removed the (.*?) part magic had after the opening of the img tag. If you think you'll need it, add it back, and change \\1 in the search pattern to \\2, and \\3 in the replacement to \\4.

Edited by rc69, 20 August 2006 - 10:54 AM.


#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 21 August 2006 - 03:14 PM

Ok thanks rc, I'll have to test it out on my localhost before I put it into the real script (I hate having to clean up the database and crap after testing querys, lol).

Looks good to me from my basic regex skills. :D
I'm just not familiar with using regex with preg_replace(), only with eregi(), lol. ;)

Edit-
It works 100% perfectly. Thanks a ton rc. :D

Edited by Demonslay, 21 August 2006 - 04:14 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users