Ok, so right now I've got this URL, which works perfectly:
$html_string = file_get_contents('https://www.123.com/' . $_GET["ticket"]);
What I want to do is to insert $_GET between the URL.
Here's what I mean:
('https://www.123.com/' . $_GET["ticket"] /url-continues-here);
I have tried everything, but can't find any solution how to do it without an error.
Concatenate the 2 strings first and after that use file_get_contents()
$html_string = 'https://www.123.com/' . $_GET["ticket"] . '/url-continues-here';
file_get_content($html_string);
Related
I need to be able to store and echo regular expressions. In my case the user enters the regex into a form and that exact sequence of characters needs to be echo-ed to the screen sometime later. The problem is that the echo changes the characters.
So for instance I have tried this
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
but when I echo it..
echo $regex;
I get...
((((amount\">\$)(.*?)(?=)
If I do this
$regex = htmlentities($regex);
I get this which helped with the missing part of the regex but not the multiple ((((
((((amount\">\$)(.*?)(?=</strong>
htmlspecialchars did not help either.
How do I get it to echo the variable exactly as it is written? And what would I need to do to store them in MySQL and retrieve them exactly as written?
EDIT - in response to some observations below, I add a bit more detail. This new example was done on a PHP 7.1 server in the cloud, Centos 7 rendered using Chrome.
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
$page_elements_regex[1][0] = $regex;
$page_elements_regex[1][1] = addslashes($regex);
$page_elements_regex[1][2] = htmlspecialchars($regex);
$page_elements_regex[1][3] = htmlentities($regex);
echo "regex " . $page_elements_regex[1][0] . "<BR>";
echo "addslashes " . $page_elements_regex[1][1] . "<BR>";
echo "htmlspecialcharacters " . $page_elements_regex[1][2] . "<BR>";
echo "htmlentities " . $page_elements_regex[1][3] . "<BR>";
Results
regex ((((amount\">\$)(.*?)(?=)
addslashes ((((amount\\\">\\$)(.*?)(?=)
htmlspecialcharacters ((((amount\">\$)(.*?)(?=</strong>)
htmlentities ((((amount\">\$)(.*?)(?=</strong>)
It is also a big clue that if you take off the first ( like this
$regex = '?<=amount\">\$)(.*?)(?=</strong>)';
The result removes the first a of amount!! Is it interpreting the regex instead of echoing it?
?(((mount\">\$)(.*?)(?=)
I have solved it, and I feel a bit foolish about the answer. My bad.
Somewhere else in my code I had this
$regex[1] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[2] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[3] = '(?<=amount\">\$)(.*?)(?=</strong>)';
I have no idea why this gave the result it did, rather than a straight up error, but once removed it all is fine. The bottom line is that both htmlspecialcharacters and htmlentities give the right answer, Lesson learnt. Check all the code, my mistake was in the use of arrays, defining $regex as an array and a variable, not as I first thought here.
i am trying to use echo inside url. i have store data from the form in database and now i am also fetching it on my page and its working well. Now i am trying to print that data i.e. number and date in url.
Is it possible and if possible please help me out
here is my data that i am fetching and it prints the output
echo $number;
echo $yyyymmdd;
and here is my url in which i want to insert ' echo $number; ' and ' echo $yyyymmdd; ' on the place of and .
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/<number>/date/<yyyymmdd>/");
I have also tried something like this but it gives error of syntex error.
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/"echo $number;"/date/"echo $yyyymmdd;"/");
Another way to add changing parameters to a URL (or string) is by using sprintf(). You define your URL and a type specifier like %d as a placeholder for numbers, and %s for strings. See the php doc for the full list of type specifiers.
$urlFormat = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/%d/date/%s/"
^ ^
Then call sprintf with the changing parameters in order of appearance.
$url = sprintf($urlFormat, $number, $yyyymmdd);
$json = file_get_contents($url);
This becomes more convenient especially if you are calling file get contents in a loop.
Create two variables and append those two inside double-quote or single quote, depending upon the quotes which you have opened and close it.
<?php
$number=123;
$yyyymmdd='2018-10-9';
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/".$number."/<number>/date/<".$yyyymmdd.">/");
?>
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/");
When you compose text, you do not need "echo" but just can write variable.
You can directly use variables in double quotes like this
file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/");
Sample code below
$number = 344;
$yyyymmdd = "20180301";
$url1 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/";
echo "url1 ".$url1."\n";
$url2 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/";
echo "url2 ".$url2. "\n";
I have three HTML form field values (name, saywords, mail) that I try to concat in php and write into one single txt file on my server:
Each value should be on a new line in the txt field, as is why I added the "\n" in the array .... where's my error?
Thanks a lot for any help!
$name = $_POST['name'];
$saywords = $_POST['saywords'];
$mail = $_POST['mail'];
$data = array($name, . "\n" $mail, . "\n" $saywords);
file_put_contents("$t.$ip.txt",$data); // Will put the text to file
You have two problems:
$data should be a string not an array
. concatenates the left hand side and the right hand side: "abc" . "def" becomes "abcdef".
putting the dot first like in . "\n" or even . "\n" $mail doesn't make sense in PHP so you'll get a parse error.
Replace your $data = line with $data = $name . "\n" . $mail . "\n" . $saywords; and you'll be good to go.
i don't see the use of array , you can concatenate them just like :
$data = $name . "\n" . $mail . "\n" . $saywords ;
It depends with the operating system of the server.
Try "\r\n" instead of "\n"
Okay, so here is my shot.
/*first of all, you should always check if posted vars are actually set
and not empty. for that, you can use an universal function "empty", which
checks if the variable is set / not null / not an empty string / not 0.
In such way you will avoid PHP warnings, when some of these variables
will not be set*/
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$saywords = !empty($_POST['saywords']) ? : $_POST['saywords'] : '';;
$mail = !empty($_POST['mail']) ? $_POST['mail'] : '';
/*Secondly, do not use \n, \r, \r\n, because these are platform specific.
Use PHP_EOL constant, it will do the job perfectly, by choosing
what type of line-break to use best.
As others mentioned - in your scenario, the string would be better solution.
Add everything into string, and then put its contents into file. Avoid using
double quotes, when you define PHP strings, and use single quotes instead - for
performance and cleaner code.
*/
$data = 'Name: '.$name.PHP_EOL.'E-Mail: '.$mail.PHP_EOL.'Message: '.$saywords.PHP_EOL.PHP_EOL;
file_put_contents($t.$ip.'.txt', $data); // Will put the text to file
By the way, I strongly suggest to also add some extra validation, before saving data to that txt file. With this code somebody can easily mess up contents of your txt file, by posting huge amounts of data with no limits.
Tips:
1) Accept only Names with limited lengths and charaters (do not allow to use special symbols or line breaks - you can also filter them out, before saving)
2) Validate e-mail which has been entered - if it is in correct format, does mx records exists for the domain of e-mail address, and so on...
3) Accept "saywords" with limited length, and if needed - deny or filter out special characters.
You will get much cleaner submissions by doing this way.
Use <br /> as you use html code
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";.
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