Results 1 to 5 of 5

Thread: RegExp

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default RegExp

    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

    PHP Code:
    <?php
    $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.
    Last edited by bluewalrus; 11-23-2010 at 09:30 PM.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.


    Code:
    <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 ) {
    			NewString--;
    			r = false;
    		}
    		if (NewString <= 0) {
    			NewString=5;
    			r=true;
    			op= "minus";
    		}
    	} else {
    		if (NewString < 6) {
    			NewString++;
    			r= false;
    		}
    		if (NewString == 6) {
    			NewString=1;
    			r=true;
    			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('inactive5').id="active";
    			document.getElementById('active').id = "inactive1";
    		} else {
    			document.getElementById('inactive1').id="active";
    			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;
    }
    </script>
    <div id="img_contain">
        <a href=""><img src="index/images/01.jpg" id="image" alt="First Figure" /></a>
    </div>
    <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>
    <div id="that"></div>
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    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:
    PHP Code:
    $pat ".*? putimage\((\d)\); return false;\"><img src=\"http:\/\/example\.com\/images\/thumbs\/active_dot\.jpg/"></a
    to
    PHP Code:
    $pat ".*? putimage\((\d)\); return false;\"><img src=\"http:\/\/example\.com\/images\/thumbs\/active_dot\.jpg/\"></a>"
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  4. The Following User Says Thank You to fileserverdirect For This Useful Post:

    bluewalrus (11-16-2010)

  5. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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

    PHP Code:
    $pat "/.*? putimage\((\d)\); return false;\"><img src=\"http:\/\/example\.com\/images\/thumbs\/active_dot\.jpg\"><\/a>/"
    Corrections to my coding/thoughts welcome.

  6. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.
    Last edited by bluewalrus; 11-23-2010 at 09:30 PM.
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •