str_replace or ereg_replace and image maps

I'm storing the html for many image maps in a mysql database, when the html is loaded I want to extend the urls for the area href's. Each image map will include several href's, each with a ref eg href="29". What I need to do when the html is loaded is extend the href to eg:

href="' . tep_href_link(FILENAME_DEFAULT, $cPath . '_29') . '"

where tep_href_link is a function and FILENAME_DEFAULT and $cPath are parameters.

The nearest I can get is:

$imagemap = str_replace('href="29"', 'href="' . tep_href_link(FILENAME_DEFAULT, $cPath . '_' . '29') . '"', $imagemap);

But '29' will be a value which differs for each area of the image map. Would ereg_replace solve the problem?
LVL 1
ncwAsked:
Who is Participating?
 
vorbeCommented:
Doing it with ereg_replace is not really an option -- it replaces only the first occurence, unlike str_replace and preg_replace. An easy way to do it with preg_replace_callback:

// this function replaces one occurence of the pattern
// in $matches[1] is the text between ""

function replace_one($matches) {
   return 'href="' . tep_href_link(FILENAME_DEFAULT, $cPath . '_' . $matches[1]) . '"';
}

// the pattern searches for href="..." and isolates the text between "" using ()

$imagemap = preg_replace_callback("/href=\"([^\"]*)\"/i", "replace_one", $imagemap);
0
 
zmagyarCommented:
More simple by using the 'e' modifier on the regexp IMO. E.g.

$imagemap = preg_replace("/href=\"([^\"]*)\"/ie", '"href=\"" . tep_href_link(FILENAME_DEFAULT, $cPath . '_' . $matches[1]) . '\"'', $imagemap );

I hope it helps,
Zoltan
0
 
ncwAuthor Commented:
vorbe, this almost worked using the following code:

function replace_one($group_cat_ids) {
   global $cPath, $page;
   return 'href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath . '_' . $page . '_' . $group_cat_ids[0]) . '"';
}


I build the array $group_cat_ids - code not shown - array values are 29, 30, 31, 32
The next line modifies each href and retains the above values in one call - don't quite understand that as the above function
only references the first value in the array $group_cat_ids[0].

$imagemap = preg_replace_callback("/href=\"([^\"]*)\"/i", "replace_one", $imagemap);

The typical result in the imagemap html is:

href="http://10.0.0.3/pewter/index.php?cPath=5_2_24_href="29";"

What is not quite correct is the _href="29"
I need to remove the href=" and second "
I see what is happening, it is retaining the search string href="29" and prefixing, where as I'm hoping to get the following:

href="http://10.0.0.3/pewter/index.php?cPath=5_2_24_29
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
ncwAuthor Commented:
zmagyar, the page would not display with your code added, may be I'm using it incorrectly or may be there is a syntax error?
0
 
ncwAuthor Commented:
vorbe, a slight change and a str_replace has got me there, but you might see a neater way:

$imagemap = preg_replace_callback("/href=\"([^\"]*)/i", "replace_one", $imagemap);

$imagemap = str_replace('_href="', '_', $imagemap);
0
 
vorbeCommented:
Actually the callback function gets a special array. At position 0 is the whole match, that is href="29" in the example. At position 1 is only 29, i.e. the part in the parentheses (if you look at the regular expression). I understood you needed only that string, not the whole href="29" string so I used that - $matches[1] (not $matches[0]).

That should be like this:

function replace_one($group_cat_ids) {
   global $cPath, $page;
   return 'href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath . '_' . $page . '_' . $group_cat_ids[1]) . '"';
}

Sorry for missing to set global those vars in my example ;)
0
 
ncwAuthor Commented:
Sorry, yes I see it does work $group_cat_ids[1], I was thinking the [1] was refering to the second value in the array values 29, 30, 31, 32, but instead it is referencing the search string. I shall never understand regular expression syntax, I shall always have to rely on the experts!
0
 
zmagyarCommented:
Sorry, the usual problem when typed directly here. The correct one is

$imagemap = preg_replace("/(href\s*=\s*\")([^\"]*)(\")/ie", '"$1" . tep_href_link(FILENAME_DEFAULT, $cPath . "_$2") . "$3"' , $imagemap );

I have tested with this script now and worked.

<?php

define("FILENAME_DEFAULT", "hehe/");
$cPath = "path/";

$imagemap = 'href = "hihi.html" href="haha.html"';

$imagemap = preg_replace("/(href\s*=\s*\")([^\"]*)(\")/ie", '"$1" . tep_href_link(FILENAME_DEFAULT, $cPath . "_$2") . "$3"' , $imagemap );

function tep_href_link($a, $b)
{
return $a . $b;
}

echo $imagemap;

?>
0
 
zmagyarCommented:
BTW I have also adjusted the regexp to be more flexible on the href tag allowing spaces before and after the = char. It can be tuned a bit more by allowing both single and double quotes if required.
0
 
ncwAuthor Commented:
zmagyar, in the imagemap there are up to 3 x href="#" to replace, hence I have built an array of those # values for vorbe's example (if that is correct), or does your example not care what the # value is, does it retain the value whatever it is?
0
 
zmagyarCommented:
It retains the value see the example script. E.g. if $imagemap = ' href="29" ... href="30"' and $cPath="path/filename.html" then the above regexp will chane it to $imagemap = ' href="path/filename.html_29" ... href="path/filename.html_30"'
0
 
ncwAuthor Commented:
Thanks. I've used vorbe's answer sucessfully. I liked zmagyar's simpler answer but I've not had time to test it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.