want to replace [img] filename [/img] tags

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gregorious
Forum Commoner
Posts: 34
Joined: Wed Aug 23, 2006 9:55 am
Location: Orlando, FL

want to replace [img] filename [/img] tags

Post by gregorious »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am developing my first PHP site [url]http://www.forthosewhowait.com/restaurant[/url] and now I want to add a feature to the Customer Management System.

I want to allow a user to add images to the text on the CATERING page. I envision the user typing  [img-r]filename.jpg[/img]  any where on the page, and then have the server display that specified image at that point on the page. I already have an image upload feature that resizes and stores the images in a folder and records image size and path in a table.

The revelant code is below. 

I can hardcode the filename in the $imagename = statement and the picture shows, but I can't seem to get the filename.jpg out of the CATERING page into the PHP code.

The image table contains path,width,height data so that is why it is included, but right now I am streamlining the effort to just getting the filename.jpg off the page and into $imagename variable code.

I think my use of REGUALR EXPRESSIONS is good "\[img-r\](.+)\[/img\]", of course that is just my opinion and I may be wrong.

Code: Select all

<?php


$sqlselect_content = @mysql_query("SELECT cater_content FROM cater");

while ($contentlist = mysql_fetch_array($sqlselect_content)) {
   $contentlist = $contentlist["cater_content"];
        
// Photos alignment  
   $contentlist = eregi_replace("\[img-r\](.+)\[/img\]","$imagename", $contentlist);


   $sqlselect_image = @mysql_query("SELECT image_name FROM image
                                     WHERE image_name='$imagename'");

   while ($imagelist = mysql_fetch_array($sqlselect_image)) {
        $imagename = htmlspecialchars($imagelist["image_name"]);
         }

   $imagename = "<img src='../images/cater/thumbs/$imagename' align='right' hspace='10' vspace='10' />";

   echo ("<br><span class='body01'>$contentlist</span><br>");

}

 ?>

Thanks in advance,
Greg


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by gregorious on Thu Aug 24, 2006 9:17 am, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd suggest looking into preg_replace_callback().. and just so you know, your [L] tag allows for XSS (cross-site scripting.)
User avatar
gregorious
Forum Commoner
Posts: 34
Joined: Wed Aug 23, 2006 9:55 am
Location: Orlando, FL

Forum Newbie, errrg-g-g

Post by gregorious »

feyd wrote:I'd suggest looking into preg_replace_callback().. and just so you know, your [L] tag allows for XSS (cross-site scripting.)
Thanks for the pointers on code submission, I am new to forums. The PHP code highlighting looks much better... now I am wondering if there is a code editor that looks as good (I am using conTEXT now).

I will look into that "callback" feature and revise my linking ([L]) tag. Thanks again.

Greg
User avatar
gregorious
Forum Commoner
Posts: 34
Joined: Wed Aug 23, 2006 9:55 am
Location: Orlando, FL

still trying to replace [img] filename

Post by gregorious »

I am developing my first PHP site http://www.forthosewhowait.com/restaurant and now I want to add a feature to the Customer Management System.

I want to allow a user to add images to the text on the CATERING page. I envision the user typing [img-r]filename.jpg[/img] any where on the page, and then have the server display that specified image at that point on the page. I already have an image upload feature that resizes and stores the images in a folder and records image size and path in a table.
Like I said, "my first PHP site" so I am still trying to get my mind wrapped around the logic.
I'd suggest looking into preg_replace_callback()..
thanks for the pointer, but preg_replace_callback() went ZOOOM - over my head! I just got introduced to REGEX and my "neural net" got a serious ping from REGEX.

After thinking about the mechanics in simple terms... I may be using the wrong approach; (if I am not mistaken) the eregi_replace is sniffing out the [img]filename[/img] tag and placing the whole tag into a variable $imagename. But all I really need is the filename; so now I am thinking the script should run something like:

Find the tag (stristr), break the tag apart (explode), use the revelant filename (db select) to return the image path, width, size. This appears reasonable enough.

Any one got any suggestions on the subject?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're going to go that route, which is perfectly fine, I'd personally do (sweatje's going to love this) 2 × strpos() + substr().
User avatar
gregorious
Forum Commoner
Posts: 34
Joined: Wed Aug 23, 2006 9:55 am
Location: Orlando, FL

Post by gregorious »

feyd wrote:(sweatje's going to love this)
Thanks, I got a good laugh from that...

Part of my wiring is "persistance" so I will look into all of the alternatives again.

Greg
User avatar
gregorious
Forum Commoner
Posts: 34
Joined: Wed Aug 23, 2006 9:55 am
Location: Orlando, FL

Post by gregorious »

First instincts are usually best followed; so here is a preg_replace_callback attempt.
This code does NOT create any errors, but it still does not replace the code with the image.
How is my syntax ?

Code: Select all

// Photo alignment  
          function putimage($matches){
          $imagename=$matches[1];
      
          $sqlselect_image = @mysql_query("SELECT image_name FROM image
                                      WHERE image_name='$imagename'");
       
          while ($imagelist = mysql_fetch_array($sqlselect_image)) {
                  $imagename = htmlspecialchars($imagelist["image_name"]);
          }                 
 
          return "<img src='../images/gallery/thumbs/$imagename' align='right' hspace='10' vspace='10' />";

          }

           preg_replace_callback("#\[img-r\](.+)\[/img\]#","putimage",$contentlist);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

lose the "@", add some var_dump() calls to see what's being passed around.

The code allows for XSS and SQL injection. If no results are found it will output a broken image tag.
User avatar
gregorious
Forum Commoner
Posts: 34
Joined: Wed Aug 23, 2006 9:55 am
Location: Orlando, FL

Post by gregorious »

lose the "@", add some var_dump() calls to see what's being passed around.

The code allows for XSS and SQL injection. If no results are found it will output a broken image tag.

Thanks for the watchful eye, these forums are encouraging. I did get help with the solution; I just needed to equate the $contentlist variable with the preg_replace_callback statement.

Code: Select all

// Photo alignment  
          function putimage($matches){ 
          $imagename=$matches[1]; 
      
          $sqlselect_image = @mysql_query("SELECT image_name FROM image 
                                      WHERE image_name='$imagename'"); 
        
          while ($imagelist = mysql_fetch_array($sqlselect_image)) { 
                  $imagename = htmlspecialchars($imagelist["image_name"]); 
          }                  
  
          return "<img src='../images/gallery/thumbs/$imagename' align='right' hspace='10' vspace='10' />"; 

          } 

           $contentlist = preg_replace_callback("#\[img-r\](.+)\[/img\]#","putimage",$contentlist);
I took a little time last night to examine the var_dump() calls; I can see how handy that can be when chasing down an error.

Thanks.
Post Reply