Answer the question
In order to leave comments, you need to log in
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';
?>
Answer the question
In order to leave comments, you need to log in
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.
Curl and the whole problem was solved... is it really necessary to download some cool parser for such nonsense... max 30 lines of code.
nokogiri is a cool parser. One of the only ones that can parse bad HTML. Although you don't need it. Take any parser!
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>';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question