Return specific HREF attribute using Xpath query - php

Having a major brain freeze, I have the following chunk of code:
// Get web address
$domQuery = query_HtmlDocument($html, '//a[#class="productLink"]');
foreach($domQuery as $rtn) {
$web = $rtn->getAttribute('href');
}
Which obviously gets the entire href attribute, however I only want 1 specific attribute within the href. I.e. If the href is: /website/product1234.do?code=1234&version=1.3&somethingelse=blaah
I only want to return the variable for "version", so wish to only return "1.3" in my example. What's most efficient way to do this?

You could use parse_url and parse_str to extract that information.

Bingo! Thanks webdestroya, parse_str is exactly what I am after:
$string="/website/product1234.do?code=1234&version=1.3&somethingelse=blaah";
parse_str($string,$return);
$version = $return['version'];
echo "Version: " . $version;
Prints:
Version: 1.3

Related

Output a part of URL in php

So, I have dynamically generated pages that follows the following format:
http://example.com/name/first_name/last_name/John%2C+Smith
What is the php code to output the last part of the url?
so, it becomes like "John, Smith".
Thank you so much.
EDIT:
I realized that the URL is ended with another / and the answers given below does not pick it up. What change should I make?
http://example.com/name/first_name/last_name/John%2C+Smith/
EDIT 2:
So, the link is dynamically generated as the following:
href="http://example.com/name/first_name/last_name/<?php echo $full_name ?>"
Split the url, get the last fragment then URL decode it:
<?
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
$end=urldecode($end);
//go on using $end then
?>
You could do this with a regex.
echo preg_replace_callback('~.*/(.*)~',
function($matches) {
return urldecode($matches[1]);
},
'http://example.com/name/first_name/last_name/John%2C+Smith');
Regex demo: https://regex101.com/r/bE3bO5/1
Output:
John, Smith
Update:
echo preg_replace_callback('~.*/(.+)~',
function($matches) {
return rtrim(urldecode($matches[1]), '/');
},
'http://example.com/name/first_name/last_name/John%2C+Smith/');
You can use parse_url with second parameter PHP_URL_PATH
$url = urldecode("http://example.com/name/first_name/last_name/John%2C+Smith");
$arr = array_filter(explode('/',parse_url($url, PHP_URL_PATH)));
print_r(end($arr));
Edited:
As per requirement for dynamic url you can use
$url = urldecode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

How can I sanitise the explode() function to extract only the marker I require?

I have some php code that extracts a web address. The object I have extracted is of the form:
WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults
Now in PHP I have called this object $linkHREF
I want to extract the id element only and put it into an array (I'm bootstrapping this process to get multiple id's)
So the command is:
$detailPagePathArray = explode("id=",$linkHREF); #Array
Now the problem is the output of this includes what comes after the id tag, so the output looks like:
echo $detailPagePathArray[0] = WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&w
echo $detailPagePathArray[1] = bf&page=1&
echo $detailPagePathArray[2] = 16123012&source=searchresults
Now the problem is obvious, where it'd firstly picking up the "id" in the "wid" marker and cutting it there, however the secondary problem is it's also picking up all the material after the actual "id". I'm just interested in picking up "16123012".
Can you please explain how I can modify my explode command to point it to the particular marker I'm interested in?
Thanks.
Use the built-in functions provided for the purpose.
For example:
<?php
$url = 'http://www.example.com?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults';
$qs = parse_url($url);
parse_str($qs['query'], $vars);
$id = $vars['id'];
echo $id; // 16123012
?>
References:
parse_url()
parse_str()
if you are sure that you are getting &id=123456 only once in your object, then below
$linkHREF = "WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults";
$str = current(explode('&',end(explode('&id', $linkHREF,2))));
echo "id" .$str; //output id = 16123012

Php string + XPath variable results in 0?

Iam new to xpath. I got a url using curl and domdocument but the problem is that the link is formated in this way: /bookstore/book.php
So then I wanna echo it to my own href link, it doesnot work ofcourse. The awnser would be to make a variable thats contains both the www.hello.com and the link I got from domdocument.
Here is my line of code:
$link = $linkquery->item(2)->nodeValue;
But if I do this it just gives me an 0
$url = "http://www.hello.com" + $link;
Any ideas? I guess I have missed something basic.
Regards
EDIT
Thanks for the help, the awnser was $url = "http://www.hello.com$link";
Isn't the string concatenation operator in PHP the dot operator .? So you want $url = "http://www.hello.com" . $link; or simply $url = "http://www.hello.com$link";.

Using str_replace to format a link into an image with embeded hyperlink

I'm not sure if I'm going about this the correct way. I would like to take a simple link, like this;
https://www.youtube.com/watch?v=examplevideo
and turn it into
<a href= 'https://www.youtube.com/embed/examplevideo' target=_blank><img src='http://img.youtube.com/vi/examplevideo/0.jpg' width='536' border='1'></a>
In the past, I've been able to change links by using a str_replace, which was very straightforward since you would pull out one pattern and just replace it with another. But, in this case, the pattern that's being kept shows up twice in the output. Is a str_replace the right way to go about it?
Here's one simple way to do it...
// $video_url = "https://www.youtube.com/watch?v=examplevideo";
$videoId = str_replace("https://www.youtube.com/watch?v=", "", $video_url);
enter code here
$videoLink = "<a href= 'https://www.youtube.com/embed/$videoId' target=_blank><img src='http://img.youtube.com/vi/$videoId/0.jpg' width='536' border='1'></a>"
Of course, if your URL is more complex (e.g. ?v=abc&t=123) then this won't work, and you would have to parse the URL more like a URL (i.e. not using str_replace).
You can use parse_url() and parse_str() to get the video ID, and then use sprintf() to build the embed code.
I've made a small function:
function getEmbedded($url) {
$parts = parse_url($url);
$parsed = parse_str($parts['query'], $params);
$result = sprintf("<a href= 'https://www.youtube.com/embed/%s'
target=_blank><img src='http://img.youtube.com/vi/%s/0.jpg'
width='536' border='1'></a>", $params['v'],$params['v']);
return $result;
}
Usage:
echo getEmbedded($url);
This is more efficient than using str_replace() and works even when there are additional query parameters in the video URL.

How to write a PHP script to find the number of indexed pages in Google?

I need to find the number of indexed pages in google for a specific domain name, how do we do that through a PHP script?
So,
foreach ($allresponseresults as $responseresult)
{
$result[] = array(
'url' => $responseresult['url'],
'title' => $responseresult['title'],
'abstract' => $responseresult['content'],
);
}
what do i add for the estimated number of results and how do i do that?
i know it is (estimatedResultCount) but how do i add that? and i call the title for example this way: $result['title'] so how to get the number and how to print the number?
Thank you :)
I think it would be nicer to Google to use their RESTful Search API. See this URL for an example call:
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:stackoverflow.com&filter=0
(You're interested in the estimatedResultCount value)
In PHP you can use file_get_contents to get the data and json_decode to parse it.
You can find documentation here:
http://code.google.com/apis/ajaxsearch/documentation/#fonje
Example
Warning: The following code does not have any kind of error checking on the response!
function getGoogleCount($domain) {
$content = file_get_contents('http://ajax.googleapis.com/ajax/services/' .
'search/web?v=1.0&filter=0&q=site:' . urlencode($domain));
$data = json_decode($content);
return intval($data->responseData->cursor->estimatedResultCount);
}
echo getGoogleCount('stackoverflow.com');
You'd load http://www.google.com/search?q=domaingoeshere.com with cURL and then parse the file looking for the results <p id="resultStats" bit.
You'd have the resulting html stored in a variable $html and then say something like
$arr = explode('<p id="resultStats"'>, $html);
$bottom = $arr[1];
$middle = explode('</p>', $bottom);
Please note that this is untested and a very rough example. You'd be better off parsing the html with a dedicated parser or matching the line with regular expressions.
google ajax api estimatedResultCount values doesn't give the right value.
And trying to parse html result is not a good way because google blocks after several search.
Count the number of results for site:yourdomainhere.com - stackoverflow.com has about 830k
// This will give you the count what you see on search result on web page,
//this code will give you the HTML content from file_get_contents
header('Content-Type: text/plain');
$url = "https://www.google.com/search?q=your url";
$html = file_get_contents($url);
if (FALSE === $html) {
throw new Exception(sprintf('Failed to open HTTP URL "%s".', $url));
}
$arr = explode('<div class="sd" id="resultStats">', $html);
$bottom = $arr[1];
$middle = explode('</div>', $bottom);
echo $middle[0];
Output:
About 8,130 results
//vKj
Case 2: you can also use google api, but its count is different:
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=ursitename&callback=processResults
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:google.com
cursor":{"resultCount":"111,000,000","
"estimatedResultCount":"111000000",

Categories