问题描述:

Is it possible to access an existing text from a html page and use it in a new html page?

I mean, for example, the music composer of Interstellar is Hans Zimmer, as written in this page

https://en.wikipedia.org/wiki/Interstellar_%28film%29

when I inspect element, it is expanded from

<html class="client-js ve-not-available" lang="en" dir="ltr">

<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-Interstellar_film skin-vector action-view" s10754256041332868330="1" mp10754256041332868330="1" fghjktghndfgt="10754256041332868330" db10754256041332868330="1" dpx10754256041332868330="1" jhjlijpomuhn_9="1">

<div id="content" class="mw-body" role="main">

<div id="bodyContent" class="mw-body-content">

<div id="mw-content-text" class="mw-content-ltr" lang="en" dir="ltr">

<table class="infobox vevent" style="width:22em;font-size:90%;">

<tbody>

<tr>

<td style="line-height:1.3em;">

<a title="Hand Zimmer" href="/wiki/Hans_Zimmer"></a>

</td>

</tr>

</tbody>

</table>

I know it can be accessed by jquery, but I need to access the text "Hans Zimmer" by php.

thanks for any kind of help

网友答案:

Are you trying to get the HTML code for a page? If so, try using file_get_contents()

<?php
$page = file_get_contents('https://en.wikipedia.org/wiki/Interstellar_%28film%29');
echo $page;
?>
网友答案:

PHP runs at server so to access values that you have available at your client, you must send a request to a .php script, by a form or by ajax, for example. PHP do not has access to client-side values, PHP actions happens before the data is sent to HTML process.

Having that said, considering you have your link as the example you wrote, you can look into URI and explode the string by "/", while the last one will be your value:

// considering the uri like: http://localhost/wiki/Hans_Zimmer
$uriRequest = $_SERVER['REQUEST_URI'];

$segments = explode("/", $uriRequest);
echo $segments[4];
网友答案:

You can use a library like this one, which uses a jquery-like syntax to manipulate the DOM.

网友答案:

It is possible to read the contents of a webpage into a PHP variable, see: PHP: how can I load the content of a web page into a variable?

Then you can use string-searching or DOM navigation functions to retrieve the information you require.

网友答案:

You can do that server side by downloading content via file_get_contents (or whatever you use) and then you can use some library for parsing/extracting content from DOM, like

http://symfony.com/doc/current/components/dom_crawler.html

Or

http://simplehtmldom.sourceforge.net

They have jQuery like selection syntax so you will not have any issue with extraction implementation.

相关阅读:
Top