sowna Posted June 17, 2012 Share Posted June 17, 2012 Hi i have to fetch the userid from the anchor tag, for example, <a href="site/user/39" title="Vinodkumar">Vinodkumar</a> i want the fetch the "39" id from the above url. Please give me the expression.... Quote Link to comment Share on other sites More sharing options...
.josh Posted June 17, 2012 Share Posted June 17, 2012 what have you tried? Quote Link to comment Share on other sites More sharing options...
PeoMachine Posted June 17, 2012 Share Posted June 17, 2012 Try to use something like: $userId = end(explode('/', 'site/user/39')); It will explode the Uri and returns the last element of the array, in this case, the Id you wanted. You can get the URI on the SERVER global. Quote Link to comment Share on other sites More sharing options...
.josh Posted June 17, 2012 Share Posted June 17, 2012 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. Quote Link to comment Share on other sites More sharing options...
ragax Posted June 17, 2012 Share Posted June 17, 2012 not just do their work for them. Amen. I burned out trying to do that a few months ago. Still recovering. Quote Link to comment Share on other sites More sharing options...
sowna Posted June 18, 2012 Author Share Posted June 18, 2012 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); } } Quote Link to comment Share on other sites More sharing options...
ragax Posted June 18, 2012 Share Posted June 18, 2012 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. Quote Link to comment Share on other sites More sharing options...
.josh Posted June 18, 2012 Share Posted June 18, 2012 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 ) Quote Link to comment Share on other sites More sharing options...
sowna Posted June 19, 2012 Author Share Posted June 19, 2012 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... Quote Link to comment Share on other sites More sharing options...
.josh Posted June 19, 2012 Share Posted June 19, 2012 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 ) Quote Link to comment Share on other sites More sharing options...
sowna Posted June 20, 2012 Author Share Posted June 20, 2012 wow super josh...its really helpful to me...thanks for help.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.