Php url content to string

Load HTML Source to String in PHP

I am trying to load the HTML source of a remote page into a string in PHP, using this awesome Galantis music video https://www.youtube.com/watch?v=5XR7naZ_zZA as an example. I then want to search for a specific div id «action-panel-details» inside the source code and confirm when it’s been found. With the code below, the entire page simply loads on the page that I’m running on my server. Is this even possible with file_get_contents()? This is the code that loads the page, video and all:

Warning: simplexml_load_string(): Entity: line 1: parser error : xmlParseEntityRef: no name in /page.php on line 5 Warning: simplexml_load_string(): ndow, document);

The type of $question and 'yes' is identical.

Discover additional insights regarding transmitting data between pages via URLs and passing variables here.

Greetings, if I comprehended correctly, commence by creating new page and assigning values to variables by utilizing form/custom . Next, utilize $question = isset($_GET['question]) ? trim($_GET['question']) : ''; to perform the same action for all variables. Finally, by employing uncomplicated if statements, load the desired content.

if ($question === 'yes' && $color === 'blue') < //showContent(A,B, D) >else if ($question === 'no' && $color === 'yellow') < //ShowContent(C,E, F) >
if (isset($_GET['question'])) < if ($_GET['question'] == 'A') < //load A content >elseif ($_GET['question'] == 'B') < //load B content >else < //load C content >> 

Get Last Part of URL PHP, Of course, if there is a query string at the end it will be included in the returned value, in which case the accepted answer is a better

Php only retrieving one string from external div from url

Allow me to clarify, you possess something akin to this:

some text 1
some text 2
some text 3
some text 4
some text 5
some text 6

If your intention is to have the text within the div elements, then make the necessary changes by substituting:

$activity = explode( '
' , $content ); $activity_second = explode("
" , $activity );
preg_match_all('#
(.+?)
#', $content, $matches);

Following the function call, the contents of $matches will be as follows.

Array ( [0] => Array ( [0] => 
some text 1
[1] =>
some text 2
[2] =>
some text 3
[3] =>
some text 4
[4] =>
some text 5
[5] =>
some text 6
) [1] => Array ( [0] => some text 1 [1] => some text 2 [2] => some text 3 [3] => some text 4 [4] => some text 5 [5] => some text 6 ) )

The necessary information is present in $matches[1].

To retrieve the complete content of divs with a particular class name, you may employ regex to capture the strings present between the tags of those divs.

preg_match_all('/
([^<]+)<\/div>/', $content, $m); print_r($m[1]);

The array $m[1] will now hold all the inner HTML strings from the divs.

The issue may be that solely the initial key from the first array is included in the second explode. Attempt the subsequent step following $activity.

$result = array(); foreach ($activity as $div)< $handle = explode("

I apologize for my initial response, as I misinterpreted your inquiry.

The regex way will work as well.

Getting data from the URL in PHP?, All the GET variables are put into a superglobal array: $_GET . You can access the value of 'page' with $_GET['page'] . For more information see PHP.

Get all urls in a string with php

 /** * * @get URLs from string (string maybe a url) * * @param string $string * @return array * */ function getUrls($string) < $regex = '/https?\:\/\/[^\" ]+/i'; preg_match_all($regex, $string, $matches); //return (array_reverse($matches[0])); return ($matches[0]); >

Employ this alternative regular expression.

$regex = "(?i)\b((?:https?://|www\d[.]|[a-z0-9.\-]+[.][a-z]/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]<>;:'\".,<>?«»“”‘’]))"; 

By using the given code, the array "urls_in_string" will be obtained and all the URLs can be found at the zero index of this array, i.e., "$urls_in_string[0]".

 $urls_in_string = []; $string_with_urls = "Worlds most popular socila networking website in https://www.facebook.com. We have many such othe websites like https://twitter.com/home and https://www.linkedin.com/feed/ etc."; $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z](\/\S*)?/im"; preg_match_all($reg_exUrl, $string_with_urls, $urls_in_string); print_r($urls_in_string); // OutPut /* Array ( [0] => Array ( [0] => https://www.facebook.com [1] => https://twitter.com/home [2] => https://www.linkedin.com/feed/ ) [1] => Array ( [0] => https [1] => https [2] => https ) [2] => Array ( [0] => [1] => /home [2] => /feed/ ) ) */ 

PHP get image url from html-string using regular expression, $html = '

(php get content ) how can i use foreach loop to rid retrieving one string

It's not entirely evident what you're looking for. Are you trying to retrieve the content of all string values between table cells labeled as

xxx

?

$url = 'https://www.w3.org/WAI/UA/2002/06/thead-test'; $content = file_get_contents($url); preg_match_all('#(.*?)#m', $content, $matches); $output = $matches[1]; var_dump($output); 

PHP get URL string parameters?, It removes the chosen language and everything after that from the url and puts that in $startUrl . Takes the original url and removes the first

Источник

Get content from a url using php

You are fetching a JavaScript snippet that is supposed to be built in directly into the document, not queried by a script. The code inside is JavaScript.

You could pull out the code using a regular expression, but I would advise against it. First, it's probably not legal to do. Second, the format of the data they serve can change any time, breaking your script.

I think you should take at their RSS feed. You can parse that programmatically way easier than the JavaScript.

Hi, Thanks for your reply but i want to know if i use their rss feed how can i pick the pun of the day dynamically (it should not repeat if i click on refresh)

That is also easy to achieve by picking a random pun from the RSS feed. It's not possible with the JS solution because it shows only the pun of the day.

I am using this code $xml = simplexml_load_file("feeds.feedburner.com/PunOfTheDay"); $items = count($xml->channel->item); echo $rand=rand(0,$items-1); $description = $xml->channel->item[$rand]->description; echo str_replace('[Click to Vote!]','',$description) Is this is correct approach

1) several local methods

2) Better Way is CURL:

echo get_remote_data('http://example.com'); // GET request echo get_remote_data('http://example.com', "var2=something&var3=blabla" ); // POST request //============= https://github.com/tazotodua/useful-php-scripts/ =========== function get_remote_data($url, $post_paramtrs=false) < $c = curl_init();curl_setopt($c, CURLOPT_URL, $url);curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); if($post_paramtrs)curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0"); curl_setopt($c, CURLOPT_COOKIE, 'CookieName1=Value;'); curl_setopt($c, CURLOPT_MAXREDIRS, 10); $follow_allowed= ( ini_get('open_basedir') || ini_get('safe_mode')) ? false:true; if ($follow_allowed)curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);curl_setopt($c, CURLOPT_REFERER, $url);curl_setopt($c, CURLOPT_TIMEOUT, 60);curl_setopt($c, CURLOPT_AUTOREFERER, true); curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');$data=curl_exec($c);$status=curl_getinfo($c);curl_close($c);preg_match('/(http(|s)):\/\/(.*?)\/(.*\/|)/si', $status['url'],$link);$data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/|\/)).*?)(\'|\")/si','$1=$2'.$link[0].'$3$4$5', $data);$data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/)).*?)(\'|\")/si','$1=$2'.$link[1].'://'.$link[3].'$3$4$5', $data);if($status['http_code']==200) elseif($status['http_code']==301 || $status['http_code']==302) < if (!$follow_allowed)> if(empty($redirURL)) > if(empty($redirURL))/si',$data,$m); if (!empty($m[1])) < $redirURL=$m[1]; >> if(!empty($redirURL))>> return "ERRORCODE22 with $url!!
Last status codes:".json_encode($status)."

Last data got
:$data";>

NOTICE: It automatically handles FOLLOWLOCATION problem + Remote urls are automatically re-corrected! ( src="https://stackoverflow.com/questions/2176180/imageblabla.png" --------> src="http://example.com/path/imageblabla.png" )

p.s.on GNU/Linux distro servers, you might need to install the php5-curl package to use it.

Источник

Читайте также:  Display text file with html
Оцените статью