Solved subtract & add numbers in url

July 19, 2011 at 13:33:49
Specs: Windows 7
Hi ! I i need your help in solving my problem
I have a webpage URL: http:/website-domain.com/index.php#page3
what i need to do is to subtract the URL number [3] -1
so if i am in #page3 a hyper link displayed [go to previous page] and it gets me to #page2
but if i am in #page2 it gets me to #page1
and so on
thank you for your help and if you want me to demonstrate what i want more just tell me

See More: subtract & add numbers in url

Report •


✔ Best Answer
July 21, 2011 at 11:19:53
No worries; if I can be of help, then you're more than welcome. :) You might try something like below to see if it will pan out. I'm not quite sure what turned sour with the other ideas... I couldn't repeat your results.

<?php

   $url = "http:/website-domain.com/index.php#page3";
   $page = preg_replace('/[a-z]/i', '', substr($url, strrpos($url, '#') + 1)); 
   settype($page, "integer");

   $previous = preg_replace('/(\d{1,2})/', $page - 1, $url);
   $current  = $url;
   $next = preg_replace('/(\d{1,2})/', $page + 1, $url);

   echo $previous . "\n" . $current . "\n" . $next;
?>

preg_replace('/(\d{1,2})/', $page + 1, $url); this line is looking to replace the current value of #page with one generated. there's no error checking, but if the result is the same as the input, something went awry. :)

I'm not sure on the origin of your links... for example, if your page had


<a href="http:/website-domain.com/index.php#page2">previous</a>
<a href="http:/website-domain.com/index.php#page3">current</a>
<a href="http:/website-domain.com/index.php#page4">next</a>
</pre>

I believe that the browser would look for "2", "3", or "4" within the current page and not request a new document. #pageN isn't passed along to php as I recall, so you couldn't use a server variable like PHP_SELF to generate new pages. I could be wrong, but I think you'd need javascript or an intermediate page to properly serve the "previous" or "next" pages if that's your goal.

HTH



#1
July 19, 2011 at 15:29:19
I'm not sure if it's what you'd need, but if your urls are consistent, then some variation of below *may* do.

<?php

   $url = "http:/website-domain.com/index.php#page3";
   $arr = split('#', $url);
   $number = preg_replace('/[a-z]/i', '', $arr[1]);
   settype($number, "integer");
   $number--;

   echo $arr[0] . "#" . "page" . $number;
?>

you could also try something like strrchr, strrpos, and substring to locate the # in the url and then the digit(s) within the resultant substring and manipulate the number as needed.

   $number = preg_replace('/[a-z]/i', '', substr($url, strrpos($url, '#') + 1));
   settype($number, "integer");
   $number += 20;  

   echo substr($url, 0, strrpos($url, '#') + 5) . $number; 

Not really tested, but hopefully either way would allow you to manipulate your url as needed. Where split, preg_replace and substr were was used, I used single quotes and not a double quote... sometimes it's not easy to tell in a reponse.

HTH


Report •

#2
July 19, 2011 at 23:16:31
thank you so much for your help i will test it and let you know

Report •

#3
July 21, 2011 at 03:52:10
Hi shutat
This is exactly what i need but the result of both codes are wrong
as the first code output is: http:/website-domain.com/index.php#page31
and the second one is: http:/website-domain.com/index.php#page241

so please if you can make anything else to help i will be graceful


Report •

Related Solutions

#4
July 21, 2011 at 09:21:53
Hello; I'll be glad to help if I can.

Could you post an example of how you were generating the links in the script prior to what I posted?


Report •

#5
July 21, 2011 at 10:47:02
Actually i could t think of anything to do that
but just if you can correct the result it will help me a lot
Thank you so much for your help

Report •

#6
July 21, 2011 at 11:19:53
✔ Best Answer
No worries; if I can be of help, then you're more than welcome. :) You might try something like below to see if it will pan out. I'm not quite sure what turned sour with the other ideas... I couldn't repeat your results.

<?php

   $url = "http:/website-domain.com/index.php#page3";
   $page = preg_replace('/[a-z]/i', '', substr($url, strrpos($url, '#') + 1)); 
   settype($page, "integer");

   $previous = preg_replace('/(\d{1,2})/', $page - 1, $url);
   $current  = $url;
   $next = preg_replace('/(\d{1,2})/', $page + 1, $url);

   echo $previous . "\n" . $current . "\n" . $next;
?>

preg_replace('/(\d{1,2})/', $page + 1, $url); this line is looking to replace the current value of #page with one generated. there's no error checking, but if the result is the same as the input, something went awry. :)

I'm not sure on the origin of your links... for example, if your page had


<a href="http:/website-domain.com/index.php#page2">previous</a>
<a href="http:/website-domain.com/index.php#page3">current</a>
<a href="http:/website-domain.com/index.php#page4">next</a>
</pre>

I believe that the browser would look for "2", "3", or "4" within the current page and not request a new document. #pageN isn't passed along to php as I recall, so you couldn't use a server variable like PHP_SELF to generate new pages. I could be wrong, but I think you'd need javascript or an intermediate page to properly serve the "previous" or "next" pages if that's your goal.

HTH


Report •


Ask Question