Php string + XPath variable results in 0? - php

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";.

Related

php preg_match get everything after match in string

Looking for how to get the complete string in a URI, after the away?to=
My code:
if (isset($_SERVER[REQUEST_URI])) {
$goto = $_SERVER[REQUEST_URI];
}
if (preg_match("/to=(.+)/", $goto, $goto_url)) {
$link = "<a href='{$goto_url[1]}' target='_blank'>{$goto_url[1]}</a>";
The original link is:
https://domain.com/away?to=http://www.zdf.de/ZDFmediathek#/beitrag/video/2162504/Verschw%C3%B6rung-gegen-die-Freiheit-%281%29
.. but my code is cutting the string after the away?to= to only
http://www.zdf.de/ZDFmediathek
You know the fix for this preg_match function to allow really every character following the away?to= ??
UPDATE:
Found out, that $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING'] is already cutting the original URL. Do you know why and how to prevent that?
try use (.*) to get all after to=
$str = 'away?to=dfkhgkjdshfgkhldsflkgh';
preg_match("/to=(.*)/", $str, $goto_url);
echo $goto_url[1]; //dfkhgkjdshfgkhldsflkgh
Instead of extracting the URL with regex from the request URI you can just get it from the $_GET array:
$link = "<a href='{$_GET['to']}' target='_blank'>{$_GET['to']}</a>";

Using Simple HTML DOM to extract an 'a' URL

I have this code for scraping team names from a table
$url = 'http://fantasy.premierleague.com/my-leagues/303/standings/';
$html = #file_get_html($url);
//Cut out the table
$FullTable = $html->find('table[class=ismStandingsTable]',0);
//get the text from the 3rd cell in the row
$teamname = $FullTable->find('td',2)->innertext;
echo $teamname;
This much works.. and gives this output....
Why Always Me?
But when I add these lines..
$teamdetails = $teamname->find('a')->href;
echo $teamdetails;
I get completely blank output.
Any idea why? I am trying to get the /entry/110291/event-history/33/ as one variable, and the Why Always Me? as another.
Instead do this:
$tdhtml = DOMDocument::loadHTML($teamdetails);
$link = $tdhtml->getElementsByTagName('a');
$url = $link->item(0)->attributes->getNamedItem('href')->nodeValue;
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
I also fail to see how your "works" code could possibly work. You don't define $teamname in there either, so all you'd never get is the output of a null/undefined variable, which is...no output all.
Marc B is right, I get that you don't have to initialize a variable, but he is saying you are trying to access a property of said variable:
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
This is essentially:
$teamname = null;
$teamname->find('a')->href;
The problem in your example is that $teamname is a string and you're treating it like a simple_html_dom_node

Regex in PHP to extract data from website

I am new to php. As a part of my course homework assignment , I am required to extract data from a website and using that data render a table.
P.S. : Using regex is not a good option but we are not allowed to use any library like DOM, jQuery etc.
Char set is UTF-8.
$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);
$patternform = '/<form(.*)<\/form>/sm';
preg_match_all($patternform ,$html,$matches);
Here regex works fine but when I apply the same regex for table tag, it return me empty array. Is there something to do with whitespaces in $html ?
What is wrong here?
The following code produces a good result:
$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);
$patternform = '/(<table.*<\/table>)/sm';
preg_match_all($patternform ,$html,$matches);
echo $matches[0][0];
Result:

How can I insert a string into another string?

I want to insert a string into another string.
I have youtube links:
http://www.youtube.com/9bZkp7q19f0
and I want to add /embed after the .com so that I can embed them on the fly.
How can I make them look like this?:
http://www.youtube.com/embed/9bZkp7q19f0
$url = str_replace("youtube.com/", "youtube.com/embed/", $url);
You can use "substr_replace" which means you are replacing text within a portion of a string.
Have a look to this, can get to know more about substr_replace,
http://php.net/manual/en/function.substr-replace.php
$link = "http://www.youtube.com/9bZkp7q19f0";
$link = str_replace("youtube.com/", "youtube.com/embed/", $link);
now
$link = "http://www.youtube.com/embed/9bZkp7q19f0";

Return specific HREF attribute using Xpath query

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

Categories