0

I have a url that looks something like this:

zigzagstudio/#!/page_wedding2

and I need to take the part of the url after the #. In fact I need to reach to page_wedding2 in order to take the number ad compare it with and id from my database. Is this possible using php? Does anyone have an example of code? I also searched for a solution using javascript but I don't know how to send it to php using javascript.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Mitik Popovici
  • 17
  • 4
  • 10

4 Answers4

2
$url = "zigzagstudio/#!/page_wedding2";
$pattern = "([\#\!\/]+(.*))";

preg_match($pattern, $url, $string);
$name = $string[1];
echo $name; // prints 'page_wedding2'
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
1
$url = 'zigzagstudio/#!/page_wedding2';
echo parse_url($url, PHP_URL_FRAGMENT);

See parse_url() docs.

Sergey Eremin
  • 10,994
  • 2
  • 38
  • 44
1

You'll need to read it in JavaScript and then pass it to your PHP.

Read it in Javascript like this:

var query = location.href.split('#');
var anchorPart = query[0];

Once you have anchorPart and parsed relevant information from it, pass it to your PHP - there may be different ways of doing this, depending on your web application.

You could make an AJAX request to page_wedding2.php and pass it any parsed information in the querystring. Use the returned HTML string in your own web page.

Edit: To clarify, the browser doesn't pass the anchor part of the URL to the server side.

Community
  • 1
  • 1
Mizuki Oshiro
  • 486
  • 5
  • 5
  • Not necessary to use JS, possible in PHP in many different ways. – Nikola K. Jul 18 '12 at 10:05
  • Please explain - you [can't read the anchor part](http://stackoverflow.com/questions/940905/can-php-read-the-hash-portion-of-the-url) of the URL in PHP, and OP is implying that it needs to be sent to the PHP page from the client side. – Mizuki Oshiro Jul 18 '12 at 10:10
0

You can use explode in PHP and split in Javascript.

PHP:

$str = 'zigzagstudio/#!/page_wedding2';
$array = explode('#', $str);
echo array_pop($array);

Javascript:

var str = 'zigzagstudio/#!/page_wedding2';
var str_array = str.split('#');
alert(str_array.pop());
Riz
  • 9,703
  • 8
  • 38
  • 54
  • but how do I get the url with the hash? because my link so far looks something like this: http://127.0.0.1/zigzagstudio/#!/page_wedding2 – Mitik Popovici Jul 18 '12 at 10:13