View Full Version : Resolved RegExp

11-14-2010, 10:58 PM
How does the RexExp work?

I'm trying to look through a series of links with images and find the one with a set image. This the the link code I'd be searching for

<a href="javascript:getimage();" onclick="putimage(2); return false;"><img src="http://example.com/images/thumbs/active_dot.jpg"></a>

In php I think it'd be

$pat = ".*? putimage\((\d)\); return false;\"><img src=\"http:\/\/example\.com\/images\/thumbs\/active_dot\.jpg/"></a>
$replace = "<a href=\"javascript:getimage();\" onclick=\"putimage($1); return false;\"><img src=\"http://example.com/images/thumbs/active_dot.jpg\"></a>";
$string = '<a href="javascript:getimage();" onclick="putimage(2); return false;"><img src="http://example.com/images/thumbs/active_dot.jpg"></a>';
$string_becomes = preg_replace($pat, $replace, $string);

I can't seem to figure out what needs to be escaped with the javascript method though.

11-16-2010, 04:14 AM
I've re-done somethings and may not need this function now. I think my current code might be more effective using the this but I'm still unable to exactly follow the functionality of it. Basically what the code I'm trying to rig up does is flip through a series of 5 images and highlight the dot that is currently being displayed. The arrows flip through or the dots can be clicked. If anyone knows of a script that is already like that, that may also be helpful. Thanks.

<script type="text/javascript">
function loadimage(which) {
var is_it = document.getElementById('image').src, NewString = is_it.replace(/.*?images\/0(\d).*/, "$1"), not_active, r;
not_active = NewString;
if (which == 0) {
if (NewString != 0 ) {
r = false;
if (NewString <= 0) {
op= "minus";
} else {
if (NewString < 6) {
r= false;
if (NewString == 6) {
op= "plus";
document.getElementById('image').src='index/images/0' + NewString + '.jpg';
if (!r) {
document.getElementById('inactive' + NewString).id="active";
document.getElementById('active').id = "inactive" + not_active;
} else {
if (op=== "minus") {
document.getElementById('active').id = "inactive1";
} else {
document.getElementById('active').id = "inactive5";
document.getElementById('that').innerHTML = "Current:" + NewString + "<br />Previous:" + not_active;
function putimage(clicked) {
document.getElementById('inactive' + NewString).id="active";
// document.getElementById('active').id = "inactive" + not_active;
<div id="img_contain">
<a href=""><img src="index/images/01.jpg" id="image" alt="First Figure" /></a>
<div id="links">
<a href="javascript:getimage();" onClick="loadimage(0); return false;"><img src="index/images/left_arrow.jpg" alt="left arrow" /></a>
<a href="javascript:getimage();" onClick="putimage(1); return false;" id="active"><img src="index/images/dot.jpg" /></a>
<a href="javascript:getimage();" onClick="putimage(2); return false;" id="inactive2"><img src="index/images/active_dot.jpg" /></a>
<a href="javascript:getimage();" onClick="putimage(3); return false;" id="inactive3"><img src="index/images/dot.jpg" /></a>
<a href="javascript:getimage();" onClick="putimage(4); return false;" id="inactive4"><img src="index/images/dot.jpg" /></a>
<a href="javascript:getimage();" onClick="putimage(5); return false;" id="inactive5"><img src="index/images/dot.jpg" /></a>
<a href="javascript:getimage();" onClick="loadimage(1); return false;"><img src="index/images/right_arrow.jpg" alt="right arrow" /></a>
<div id="that"></div>

11-16-2010, 04:18 AM
Well, I don't really understand your problem, others may be able to help with that, but I do see a php error in your first post:

$pat = ".*? putimage\((\d)\); return false;\"><img src=\"http:\/\/example\.com\/images\/thumbs\/active_dot\.jpg/"></a>

$pat = ".*? putimage\((\d)\); return false;\"><img src=\"http:\/\/example\.com\/images\/thumbs\/active_dot\.jpg/\"></a>";

11-16-2010, 04:34 AM
Oh yea, there's a few problems there kinda re-did it though because I couldn't figure out the regexs with javascript. To be complete with the post though the correct full pattern for php would've been

$pat = "/.*? putimage\((\d)\); return false;\"><img src=\"http:\/\/example\.com\/images\/thumbs\/active_dot\.jpg\"><\/a>/";

11-22-2010, 02:36 AM
I've returned to this with a different question but the samish code. I'm not sure how the to make the "links" part global so I've defined each in their functions. The problem with the page currently is only when using the arrows if you go past the last image it loses track of where it is. I think it is from my attempt to make a work. My attempt was if on 9 and attempt to advance set don't advance set to 1 instead and unset 9 and if on 1 and try and devance(word?) set to 9 and unset 1.

Math was off removed math and went by ID names.