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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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,
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:


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:

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

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?
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);
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 ;)
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!
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.


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;

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.
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?
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"'
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.