General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 3rd, 2003, 01:03 AM
Robert K S Robert K S is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Cleveland OH USA
Posts: 8 Robert K S User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Robert K S
Talking explode() doesn't work when preg_replace() calls a function

I'm trying to write a PHP routine that parses web pages and replaces a custom HTML tag, <picture>, with an <img> tag that includes a link to a resized cached picture generated by the same tag replacement function. I'm using preg_replace() to do the <picture>-to-<img> replacement. But, quite strangely inside the part of the routine that parses the <picture> tag attributes, the explode() function decides he's tired of working and he walks right off the job.

In the example code below, both Method 1 and Method 2 should echo the same $content, but for some mysterious reason, they don't! They SHOULD be functionally equivalent, but they're not: in Method 1, the explode() function gets lazy and refuses to do its job. Please try running this sample code so that you may marvel at the inexplicable result. Can anybody tell me what the heck is going on?

All the best,
Robert K S


PHP Code:
<?

function preg_split_then_explode($picture_tag_attributes) {
  list(
$picture_id_and_size$picture_replacement) = preg_split('/\s+/'$picture_tag_attributes);
  list(
$picture_id$picture_size) = explode("_"$picture_id_and_size);
  return(
'$picture_id = '.$picture_id.'<br>$picture_size = '.$picture_size);
}

// Method 1: preg_replace() calls a function
$content '<picture 125_small>';
$content preg_replace('/((<\s*picture\s*)([^>]*)(>))/i'preg_split_then_explode("$3"), $content);
echo 
$content;

echo 
'<p>';

// Method 2: bypassing the preg_replace step
$content '125_small';
list(
$picture_id_and_size$picture_replacement) = preg_split('/\s+/'$content);
list(
$picture_id$picture_size) = explode("_"$picture_id_and_size);
$content '$picture_id = '.$picture_id.'<br>$picture_size = '.$picture_size;
echo 
$content;

// Method 2 works, but in Method 1, the explode() function decides not to work!  Why??

?>

Last edited by Robert K S : March 3rd, 2003 at 02:16 AM.

Reply With Quote
  #2  
Old March 3rd, 2003, 01:03 AM
Robert K S Robert K S is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Cleveland OH USA
Posts: 8 Robert K S User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Robert K S
preg_replace_callback() is the solution to the problem, illustrated in this fixed example code:

PHP Code:
<?

function preg_split_then_explode($picture_tag_attributes) {
  
$picture_tag_attributes $picture_tag_attributes[3];
  list(
$picture_id_and_size$picture_replacement) = preg_split('/\s+/'$picture_tag_attributes);
  list(
$picture_id$picture_size) = explode("_"$picture_id_and_size);
  return(
'$picture_id = '.$picture_id.'<br>$picture_size = '.$picture_size);
}

// Method 1: preg_replace_callback() calls a function
$content '<picture 125_small>';
$content preg_replace_callback('/((<\s*picture\s*)([^>]*)(>))/i''preg_split_then_explode'$content);
echo 
$content;

echo 
'<p>';

// Method 2: bypassing the preg_replace step
$content '125_small';
list(
$picture_id_and_size$picture_replacement) = preg_split('/\s+/'$content);
list(
$picture_id$picture_size) = explode("_"$picture_id_and_size);
$content '$picture_id = '.$picture_id.'<br>$picture_size = '.$picture_size;
echo 
$content;

// Now Method 1 and Method 2 echo the same thing!

?>

Last edited by Robert K S : March 3rd, 2003 at 02:16 AM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > explode() doesn't work when preg_replace() calls a function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway