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
<?
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??
?>