Jump to content

Get the userid in the URL


sowna

Recommended Posts

He can't get the value from the server superglobal...that only contains info about the request url.  He is trying to extract a url from a string of html code. 

 

My question is, what has he tried?  I ask because we are here to help people in their efforts, because our goal is to teach people how to do stuff, not just do their work for them. 

Link to comment
Share on other sites

Sorry guys for late reply...had some network issue,

 

i tried below code, i know this is very bad code which i wrote...this was working fine when i use anchor tag without title attribute...and i want to fetch uid in a simple way...that's why i asked help from you people...

 

$strArray = explode('<a href=',$str);
$finalArray = array();
if(count($strArray)>1){

	$firstArray = array();
	foreach($strArray as $key => $value){
		if(strlen(trim($value))>0){
			preg_match( "/user/", $value , $matches, PREG_OFFSET_CAPTURE);
			if(count($matches)>0){
				$subpart = explode('">',$value);
				$firstArray[]=$subpart[0];
			}

		}
	}

	if(count($firstArray)>0){
		$firstArray = implode("/",$firstArray);
		$firstArray = explode("/",$firstArray);

		$finalArray = array();
		foreach($firstArray as $key => $value){
			if (is_numeric($value)) {
				$finalArray[]=$value;
			}
		}
		return ($finalArray);
	}
}

Link to comment
Share on other sites

Hi sowna,

 

"/user/"

 

Your regex needs to grab everything after user-slash and before the closing quotes. At the moment, it just matches "user" (since your slashes are delimiters, as opposed to the slashes in the code).

 

Here are two ways to do it ($regex and $regex2).

 

$regex='~stuff/([^"]+)~';
$regex2='~stuff/\K[^"]+~';
$string='my stuff/85"';
if(preg_match($regex,$string,$m)) echo $m[1].'<br />';
if(preg_match($regex2,$string,$m)) echo $m[0].'<br />';

 

This code will echo 85 twice. Run this to see how it works.

 

Hint: replace stuff with user and you will be in good shape.

 

Let me know if you have any questions.

In the meantime:

[^"] means any character that is not a double quote, the plus means "match as many of these characters as possible". The first example uses a capture (with the parentheses), the second uses \K, a feature of PCRE that resets the reported match.

 

:)

Link to comment
Share on other sites

sowna, judging by the code you posted, it looks like you are working with an html document, perhaps retrieved from a file_get_contents or cURL call, and you are trying to extract the user id from links on the page.  When trying to parse html, it is safer to use a DOM Parser than regex. 

 

example:

 

// example content to parse.  You didn't post that part of your script, but you probably got this from a file_get_contents call or something. 
$content = <<<EOF
<html>
<head></head>
<body>
<a href="site/user/36" title="Vinodkumar">Vinodkumar 36</a>
<a href="site/user/37" title="Vinodkumar">Vinodkumar 37</a>
<a href="site/user/38" title="Vinodkumar">Vinodkumar 38</a>
<a href="site/user/39" title="Vinodkumar">Vinodkumar 39</a>
<a href="somepage.php">some page</a>
</body>
</html>
EOF;


// create a new DOM object
$doc = new DOMDocument();
// load the html content to be parsed
$doc->loadHTML($content);
// get all the anchor tags
$links = $doc->getElementsByTagName('a');
// for each anchor tag...
foreach ($links as $link) {
  // get the href attribute
  $href = $link->getAttribute('href');
  // if href is the right link ...
  if ( stripos($href,'/user/')!==false ) 
    // user id is the last / delimited value in $href, grab it and put into array of found user ids
    $user_ids[] = array_pop(explode('/',$href));
} // end foreach anchor tag

// example showing output of found user ids
echo "<pre>"; print_r($user_ids); echo "</pre>";

 

output:

 

Array
(
    [0] => 36
    [1] => 37
    [2] => 38
    [3] => 39
)

Link to comment
Share on other sites

Thanks josh and ragax...

 

its working fine....

 

could u please tel me how to fetch the entire '<a href="site/user/36" title="Vinodkumar">Vinodkumar 36</a>' in the loop??

 

i searched google but didn't found any function...

 

Link to comment
Share on other sites

Okay, I know the DOM object is complex and scary at first, so I'll give you some more examples.  After that, you are on your own...follow the provided link, RTFM! :)

 

<?php
// example content to parse.  You didn't post that part of your script, but you probably got this from a file_get_contents call or something. 
$content = <<<EOF
<html>
<head></head>
<body>
<a href="site/user/36" title="Vinodkumar">Vinodkumar 36</a>
<a href="site/user/37" title="Vinodkumar">Vinodkumar 37</a>
<a href="site/user/38" title="Vinodkumar">Vinodkumar 38</a>
<a href="site/user/39" title="Vinodkumar">Vinodkumar 39</a>
<a href="somepage.php">some page</a>
</body>
</html>
EOF;


// create a new DOM object
$doc = new DOMDocument();
// load the html content to be parsed
$doc->loadHTML($content);
// get all the anchor tags
$links = $doc->getElementsByTagName('a');
// for each anchor tag...
foreach ($links as $k => $link) {
  
  // output the full, raw link tag.
  // wrapped in htmlentities() for display purposes
  $rawTag = $doc->saveXML($link);
  echo htmlentities($rawTag) . "<br/><br/>";

  // output the innerHTML of the link
  echo "link value : " . $link->nodeValue . "<br/><br/>";

  // alternative way to output the innerHTML of the link
  echo "link value : " . $link->textContent . "<br/><br/>";

  // output a specified attribute
  echo "href : " . $link->getAttribute('href') . "<br/><br/>";			

  // output all of the attrib='value' in the current link
  // (first it is put into an associative array for convenience)
  $link_attributes = array();
  foreach($link->attributes as $attribute) {
    $link_attributes[$attribute->name] = $attribute->value;
  }
  echo "<pre>";print_r($link_attributes); echo "</pre>";

  echo "<hr>";
} // end foreach anchor tag

?>

 

output:

<a href="site/user/36" title="Vinodkumar">Vinodkumar 36</a>

link value : Vinodkumar 36

link value : Vinodkumar 36

href : site/user/36

Array
(
    [href] => site/user/36
    [title] => Vinodkumar
)
<a href="site/user/37" title="Vinodkumar">Vinodkumar 37</a>

link value : Vinodkumar 37

link value : Vinodkumar 37

href : site/user/37

Array
(
    [href] => site/user/37
    [title] => Vinodkumar
)
<a href="site/user/38" title="Vinodkumar">Vinodkumar 38</a>

link value : Vinodkumar 38

link value : Vinodkumar 38

href : site/user/38

Array
(
    [href] => site/user/38
    [title] => Vinodkumar
)
<a href="site/user/39" title="Vinodkumar">Vinodkumar 39</a>

link value : Vinodkumar 39

link value : Vinodkumar 39

href : site/user/39

Array
(
    [href] => site/user/39
    [title] => Vinodkumar
)
<a href="somepage.php">some page</a>

link value : some page

link value : some page

href : somepage.php

Array
(
    [href] => somepage.php
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.