V
V
vibrax2020-10-24 12:43:05
Arduino
vibrax, 2020-10-24 12:43:05

How to insert variables from one PHP file into different pages of the site?

There is a set of texts for a ten-page site, I want to edit them in one place, and not jump through page templates. To do this, apparently you need to make a PHP file with something like this?

<?php
    $index_1 = 'Some text 1';
    $index_2 = 'Some text 2';
    $page_1 = 'Text1';
    $page_2 = 'Text2';
?>


Are there any errors here and how then to load it from the file on each necessary page and display it? Thank you.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
VovanZ, 2016-04-15
@VovanZ

1. Retrieve the page with file_get_contents or curl
2. Here is a list of XML parsers for PHP .
Unfortunately, I don’t know anything about them (I’ve never written anything similar in PHP, I myself would choose python and lxml for this task), but one of them definitely knows how to parse html :)
In general, you could make one more page, and give the necessary data there in json, it is much easier to parse it.
json_decode - and you have a ready-made object / array (optional) with the necessary data.

A
Alexander Sisyukin, 2016-04-16
@Caarl

Curl and the whole problem was solved... is it really necessary to download some cool parser for such nonsense... max 30 lines of code.

V
vdem, 2016-04-15
@vdem

simplehtmldom.sourceforge.net

V
Vanya Zyuzgin, 2016-04-15
@site2life

nokogiri is a cool parser. One of the only ones that can parse bad HTML. Although you don't need it. Take any parser!

A
Andrew, 2016-04-22
@ntzch

For such a page, pure php with regular expressions is enough ...
One of the examples:

$page = file_get_contents('http://192.168.11.161');
preg_match_all('!(((\w+ )?\w+) \([A-Z%]\)): ([0-9.]+)!', $page, $matches);

$title = $matches[1];
$value = $matches[4];
$result = array_combine($title, $value);


echo '<pre>';
print_r($result);
echo '</pre>';

If you need it in some other form, then it’s enough to play around with the regular expression and process the keys / values ​​as it should ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question