Re: [PHP] problem matching multiple times

2003-07-18 Thread Vince LaMonica
On Fri, 18 Jul 2003, Curt Zirzow wrote:

} > Do you mean: 
} > 
} > $patterns[3] = "/###Image($count+1)###/";
} > $replacements[3] = "$sel_image = "SELECT * from ppd_photos where ppd_id = '$_GET[ppdid]' AND  
place_holder = '$thephoto[0]'";

} What does this return?
} 
} I'm a bit confused right now..

The query simply returns one row with all the htmlstuff [width, height, 
caption, image_holder, etc] for one image. In otherwords, $message might 
contain:
   
 
-
Hello world, look at my [b]cat[/b]: ###Image1### Nice, huh?
And now look at my [b]dog[/b]: ###Image2### Not so nice, eh?
-

I'm using a big preg_replace call to replace any of that custom formatting 
tag [eg: [b][/b]] as well as the ###ImageX### tags. Instead of replacing 
###Image1### with some formatting code, I need to look up ###Image1### in 
the database, which stores the formatting codes. So the in the above 
query, $thephoto[0] contains '###Image1###', since it is the first photo 
on the page. However, when preg_replace finds a 2nd photo, with a 
image_holder of ###Image2###, the select statement is still run against 
the first image place holder, ###Image1###. So I need to advance the 
counter before running the SQL statement

If I leave out the sql stuff and do something like this:

$patterns[3] = "/###Image(\d+)###/si";
$replacements[3] = "My Photograph\$1";

Then the page sucessfully substitutes "My Photograph1" for every place 
that $message contained ###Image1### and "My Photograph2" for every place 
that $message contained ###Image2###. 

So I need to somehow run preg_match on that part of $message in order to 
extract ###Image1###, ###Image2###, ###Image3###, etc from the message and 
look them up in the database. I think. :-\

} after the user enters in his stuff, he wants, in your CMS and before you
} save it, parse it for all the special tags you have and keep track of it
} somewhere.  That way when you output it (in this script) you only need
} to apply the necessary pattern/replacements before outputing it to the
} browser.  

Good idea! Thanks very much!

Thanks again for your help, and time, with this. I do appreciate it.

/vjl/

-- 
Vince LaMonica   UC Irvine,  School  of  Social Ecology
 W3 Developer   <*>  116 Social Ecology I, Irvine, CA 92697
 [EMAIL PROTECTED]  http://www.seweb.uci.edu/techsupport

Linux price to performance ratio: Error: Divide by zero. Continue?(Y/N)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] problem matching multiple times

2003-07-18 Thread Curt Zirzow
Vince LaMonica <[EMAIL PROTECTED]> wrote:
> On Fri, 18 Jul 2003, Curt Zirzow wrote:
> 
> [snip]
> } 
> } The only solution I can think of is while your looping through the
> } images they have  build more pattern/replacemen array items
> } 
> } foreach (row in db)
> }$pattern[] = /###Image($count+1)###
> }$$replacements[] = "$image_name";
> 
> Do you mean: 
> 
> $patterns[3] = "/###Image($count+1)###/";
> $replacements[3] = " 
> The problem with this is that I don't know how many rows are going to come 
> back, since the first/only SQL statement just returns one row [whatever 
> the current image placeholder is...in this case, ###Image1###. So I need 
> to loop before I run the SQL statements. 


>$sel_image = "SELECT * from ppd_photos where ppd_id = '$_GET[ppdid]' AND > 
>place_holder = '$thephoto[0]'";

What does this return?

I'm a bit confused right now..


> 
> Also, using $patterns[3] again overwrites the first call to it [the one 
> you recommended i re-write for readablity]. I need to make sure the 
> $replacements[3] contains all of the html stuff, so it needs a pattern for 
> the preg_replace call later in the function:

by using the patters[] and replacements[] it will avoid the overwriting.

> 
> $message = preg_replace($patterns, $replacements, $message);


> 
> Thanks for your help with this!
> 
> /vjl/

oh, i'm feeling helpful today.. another suggestion for performance:

after the user enters in his stuff, he wants, in your CMS and before you
save it, parse it for all the special tags you have and keep track of it
somewhere.  That way when you output it (in this script) you only need
to apply the necessary pattern/replacements before outputing it to the
browser.  

Curt.
-- 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] problem matching multiple times

2003-07-18 Thread Vince LaMonica
On Fri, 18 Jul 2003, Curt Zirzow wrote:

} btw, you know you can do this in one step:
} 
} $message = preg_replace("/\[(i|b)\](.*?)\[\/(i|b)\]/si", "<\$1>\$2", $message);

Doh! Thanks for the tip...that'll save some lines of code :)

} > $patterns[3] = "#\#\#\#Image(.*?)\#\#\##si"; 
} > // this matches just fine strings like ###Image1### and ###Image2###
} 
} I would change mabey make sure you catching a digit and the serpator for
} readabilty /###Image(\d+)###/si

Ok. Changed it and you're right - much easier to understand what the 
patern is being searched.

} > [...] 
} >  if ($_GET[ppdid] != "") { // the ppdid var is passed in the URL 
} >  $sel_image = "SELECT * from ppd_photos where ppd_id = '$_GET[ppdid]' AND 
place_holder = '$thephoto[0]'";
} > // in this case, the page with a ppdid of '3' has 2 images, who's 
} > // placeholders are: ###Image1### and ###Image2###
} >  $sel_image_result = safe_query($sel_image);
} >  $sel_image_row = mysql_fetch_array($sel_image_result);
} >  $image_id = $sel_image_row["image_id"];
} >  $image_name = $sel_image_row["image_name"];
} >  $caption = $sel_image_row["caption"];
} >  $width = $sel_image_row["width"];
} >  $height = $sel_image_row["height"];
} >  $image_alignment = $sel_image_row["image_alignment"];
} >  $replacements[3] = "  116 Social Ecology I, Irvine, CA 92697
 [EMAIL PROTECTED]  http://www.seweb.uci.edu/techsupport

 Your mouse has moved. Windows NT must be restarted for the
 change to take effect. Reboot now? [ OK ]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] problem matching multiple times

2003-07-17 Thread Curt Zirzow
Vince LaMonica <[EMAIL PROTECTED]> wrote:
> Hi All,

hello

 
> [...]
> function vjencode($message) {
>  
>  $message = " " . $message ;
> 
> ##--- [b] and [/b] for bolding text.
>  $message = preg_replace("/\[b\](.*?)\[\/b\]/si", "\\1", $message);
> 
> ##--- [i] and [/i] for italicizing text.
>  $message = preg_replace("/\[i\](.*?)\[\/i\]/si", "\\1", $message);

btw, you know you can do this in one step:

$message = preg_replace("/\[(i|b)\](.*?)\[\/(i|b)\]/si", "<\$1>\$2", $message);

> [...] 
> ##--- display photos [having problems with this]
> $patterns[3] = "#\#\#\#Image(.*?)\#\#\##si"; 
> // this matches just fine strings like ###Image1### and ###Image2###

I would change mabey make sure you catching a digit and the serpator for
readabilty /###Image(\d+)###/si

> [...] 
>  if ($_GET[ppdid] != "") { // the ppdid var is passed in the URL 
>  $sel_image = "SELECT * from ppd_photos where ppd_id = '$_GET[ppdid]' AND 
> place_holder = '$thephoto[0]'";
> // in this case, the page with a ppdid of '3' has 2 images, who's 
> // placeholders are: ###Image1### and ###Image2###
>  $sel_image_result = safe_query($sel_image);
>  $sel_image_row = mysql_fetch_array($sel_image_result);
>  $image_id = $sel_image_row["image_id"];
>  $image_name = $sel_image_row["image_name"];
>  $caption = $sel_image_row["caption"];
>  $width = $sel_image_row["width"];
>  $height = $sel_image_row["height"];
>  $image_alignment = $sel_image_row["image_alignment"];
> // replacements[3] is all one one line -sorry about word wrap!
>  $replacements[3] = " width=\"$width\" align=\"$image_alignment\"> src=\"/uimages/$image_name\" 
> height=\"$height\" width=\"$width\" border=1> class=\"caption\">$caption";
> }

The only solution I can think of is while your looping through the
images they have  build more pattern/replacemen array items

foreach (row in db)
   $pattern[] = /###Image($count+1)###
   $$replacements[] = "$image_name";

Curt
-- 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] problem matching multiple times

2003-07-17 Thread Vince LaMonica
Hi All,

I have a content management system that I'm building and I'm having issues 
matching a variable that needs to be replaced with rows from a database. 
Basicly I have something setup similar to bbcode, where a user enters 
html-like text for formatting - eg: [b]bold text[/b]. I'm now adding the 
ability to link photos in this formatted text. 

In order to display the page using html, I obviously need to convert the 
special tags to their real ones. With the images, I'm actually pulling 
data [image name, width, height, image_placer, alignment, etc] from a 
database. There can be more than 1 image on a page [and more than 10, 
actually]. The user would simply type into the CMS editor something like, 
"Here is a photo of my dog ###Image1###" The ###Image1### is a 
placeholder. The user gets assigned a placeholder for each image they 
upload. They then assign different parameters to that image [a caption, 
and center/left/right alignment].

In order to display this data, I call a function called vencode() on the 
page that a user will see:



The entire page's content section is inside $message. The vencode() 
function looks like this:

function vjencode($message) {
 
 $message = " " . $message ;

##--- [b] and [/b] for bolding text.
 $message = preg_replace("/\[b\](.*?)\[\/b\]/si", "\\1", $message);

##--- [i] and [/i] for italicizing text.
 $message = preg_replace("/\[i\](.*?)\[\/i\]/si", "\\1", $message);


##--- Patterns and replacements for URL and email tags..   
$patterns = array();
$replacements = array();

##--- [url]://www.seweb.uci.edu[/url] code..
$patterns[0] = "#\[url\]([a-z]+?://){1}(.*?)\[/url\]#si";
$replacements[0] = '\1\2';


##--- [url]www.seweb.uci.edu[/url] code.. (no :// prefix).
$patterns[1] = "#\[url\](.*?)\[/url\]#si";
$replacements[1] = 'http://\1";>\1';

##--- [url=://www.seweb.uci.edu]Social Ecology Home[/url] code..
$patterns[2] = "#\[url=([a-z]+?://){1}(.*?)\](.*?)\[/url\]#si";
$replacements[2] = '\3';

##--- display photos [having problems with this]
$patterns[3] = "#\#\#\#Image(.*?)\#\#\##si"; 
// this matches just fine strings like ###Image1### and ###Image2###


 if ($_GET[ppdid] != "") { // the ppdid var is passed in the URL 
 $sel_image = "SELECT * from ppd_photos where ppd_id = '$_GET[ppdid]' AND place_holder 
= '$thephoto[0]'";
// in this case, the page with a ppdid of '3' has 2 images, who's 
// placeholders are: ###Image1### and ###Image2###
 $sel_image_result = safe_query($sel_image);
 $sel_image_row = mysql_fetch_array($sel_image_result);
 $image_id = $sel_image_row["image_id"];
 $image_name = $sel_image_row["image_name"];
 $caption = $sel_image_row["caption"];
 $width = $sel_image_row["width"];
 $height = $sel_image_row["height"];
 $image_alignment = $sel_image_row["image_alignment"];
// replacements[3] is all one one line -sorry about word wrap!
 $replacements[3] = "$caption";
}

$message = preg_replace($patterns, $replacements, $message);

return $message;
} //end of function

In the case of the page with the ppdid of '3', there are two images 
assigned to that page. However, when running the above code, I two photos 
of the first listed image! Eg, the text in $message looks like this:

-
Hello world, look at my [b]cat[/b]: ###Image1### Nice, huh?
And now look at my [b]dog[/b]: ###Image2### Not so nice, eh?
-

The substitution code also translates linebreaks as s and such, but I 
didn't think I needed to include the entire list of substitutions it 
makes. 

I'm sure I need a while loop in there somewhere, but I can not figure out 
where. I know the code is  looping through the other substitutions ok, 
since all text that should be bold is, and all links that need to be made 
into valid URIs are. 

The db query does run fine if running within mysql, and substituting 
###Image1### and ###Image2### for $thephoto[0]. I know that $thephoto[1] 
simply contains a "1" [since that is the value of $patterns[3]].

Any tips about where I need to loop and how, would be most appreciated! 

/vjl/

-- 
Vince LaMonica   UC Irvine,  School  of  Social Ecology
 W3 Developer   <*>  116 Social Ecology I, Irvine, CA 92697
 [EMAIL PROTECTED]  http://www.seweb.uci.edu/techsupport

 If it be now, 'tis not to come; if it be not to come, it will be now;
 if it be not now, yet it will come: the readiness is all." 
   -- William Shakespeare, "Hamlet." 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php