Include with str_replace function? - php

I want to create a page with include function. This page should to grab another site and can change text or code with this function str_replace. I hope that it would be possible. I have written this code, but unfortunately it does not work:
<?php
$text = include('http://www.example.com/index.html');
$text = str_replace("<div id=\"hercss\">Hello.</div>", "<div id=\"mycss\">Welcome!</div>", $text);
echo $text;
?>
Maybe you have a solution? It would be fully appreciated by you.

Try this:
<?php
$url = "http://www.example.com/index.html";
$text = file_get_contents($url);
$text = str_replace("<div id=\"hercss\">Hello.</div>", "<div id=\"mycss\">Welcome!</div>", $text);
echo $text;
?>
Include only works with relative links I believe? The function file_get_contents works across domains

Related

json_encode not working with strip_tags

I want to insert some input a list into JSON but it looks very strange. I use strip_tags to fix a memory leak. Here is the code and the outputted JSON
Code:
<?php
echo '<meta charset="utf-8">';
include_once('libs/simplehtmldom/simple_html_dom.php');
$html = file_get_html('https://hugo.events/event/2017');
// Find all links
foreach($html->find('ul#EventLandinLineUpList') as $element)
$encoded = strip_tags($element);
echo json_encode($encoded);
And this is the JSON that has been outputted:
"\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tFEDDE LE GRAND\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tKENSINGTON\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tTYPHOON\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tSUNDAY SUN\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tBL\u00d8F\n\t\t\t\t\t\t\t\t\t\t\t\t\t"
I hope someone can help me. Thanks in advance
You can use trim() to remove the whitespace there.
Eventually I used: preg_replace.
This was the final code:
<?php
echo '<meta charset="utf-8">';
include_once('libs/simplehtmldom/simple_html_dom.php');
$html = file_get_html('https://hugo.events/event/2152');
// Find all links
foreach($html->find('ul#EventLandinLineUpList') as $element)
$stripped = strip_tags($element);
$stripped = preg_replace('/\s+/', ' ', $stripped);
echo json_encode($stripped);

PHP include HTML and echo out variable

I am working on a script with templates. So I have this PHP code:
<?php
$string = "TEST";
echo(file_get_contents('themes/default/test.html'));
?>
And I have this HTML (the test.html file):
<html>
<p>{$string}</p>
</html>
How can I make PHP actually display the variable inside the curly brackets? At the moment it displays {$string}.
P.S:
The string might also be an object with many many variables, and I will display them like that: {$object->variable}.
P.S 2: The HTML must stay as it is. This works:
$string = "I'm working!"
echo("The string is {$string}");
I need to use the same principle to display the value.
You can use the following code to achieve the desired result:
<?php
$string = "TEST";
$doc = file_get_contents('themes/default/test.html'));
echo preg_replace('/\{([A-Z]+)\}/', "$$1", $doc);
?>
P.S. Please note that it will assume that every string wrapped in { }
has a variable defined. So No error checking is implemented in the code above. furthermore it assumes that all variables have only alpha characters.
If it is possible to save your replacees in an array instead of normal variables you could use code below. I'm using it with a similar use case.
function loadFile($path) {
$vars = array();
$vars['string'] = "value";
$patterns = array_map("maskPattern", array_keys($vars));
$result = str_replace($patterns, $vars, file_get_contents($path));
return $result;
}
function maskPattern($value) {
return "{$" . $value . "}";
}
All you PHP must be in a <?php ?> block like this:
<html>
<p><?php echo "{" . $string . "}";?></p>
</html>
If you know the variable to replace in the html you can use the PHP function 'str_replace'. For your script,
$string = "TEST";
$content = file_get_contents('test.html');
$content = str_replace('{$string}', $string, $content);
echo($content);
It's simple to use echo.
<html>
<p>{<?php echo $string;?>}</p>
</html>
UPDATE 1:
After reading so many comments, found a solution, try this:
$string = "TEST";
$template = file_get_contents('themes/default/test.html', FILE_USE_INCLUDE_PATH);
$page = str_replace('{$string}',$string,$template);
echo $page;

Error using preg_relace to change url youtube?

I have a sample code:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$pattern = '/http:\/\/www\.youtube\.com\/watch\?(.*?)v=([a-zA-Z0-9_\-]+)(\S*)/i';
$replace = $pattern.'&w=550';
$string = preg_replace($pattern, $replace, $url);
?>
How to result is http://www.youtube.com/watch?v=KTRPVo0d90w&w=550
You can just append using the . operator:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$string = $url.'&w=550';
?>
Use preg_match instead:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w&s=222';
$pattern = '/v=[^&]+/i';
preg_match($pattern, $url, $match);
echo 'http://www.youtube.com/watch?'.$match[0].'&w=550';
?>
Like below?
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$bit = '&w=550';
echo "${url}${bit}";
Don't get me wrong, I'm not looking to gain any points here, but just thought I would add to this question and include a few options. I love toying with ideas like this every once in a while.
Using jh314's idea to concatenate the strings, thought that this could be used for future use, to actually replace a string inside the video's YouTube number, should the occasion ever present itself.
Such as $number for instance.
<?php
$url = 'http://www.youtube.com/watch?v=';
$number = 'KTRPVo0d90w';
$string = $url.$number.'&w=550';
// Output to screen
echo $string;
echo "<br>";
// Link to video
echo "Click for the video";
?>
The same could easily be done for the video's width.

Extract the text from webpage

In this test.php page i have this line of text
server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb
in this other test1.php?id=token page i have this php code runing
<?php
$Text=file_get_contents("./test.php");
if(isset($_GET["id"])){ $id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid; } else { echo ""; } ?>
i need only the token
eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb
to be show on test1.php?id=token
if in test.php the token looks like this
token='eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb'
it works.
i needet to work from onother web page
$str = 'server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb';
parse_str($str, $vars);
$token = $vars['token'];
using with preg_match will help you .
$string ='server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb';
preg_match('/token=([a-f0-9]+)/i',$string,$matches);
echo $matches[1];
this will return you :
'eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb'
I'd recommend you to use preg_match instead of preg_match_all
Try this regex:
$regex = "/&?token=([a-f0-9]*)&?/;

Disable javascript on page

Is there any way to disable javascript when generating a page with php?
Code:
<?php
$url = 'http://www.kupime.com/';
$data = file_get_contents($url);
$data = '<head><base href='.$url.' target="_blank" /></head>'.$data;
echo $data;
?>
Use regular expressions to remove everything that is inside <script> tags.
$text = preg_replace('/<script.+?<\/script>/im', "", $text);
check out this bookmarklet that prints all script elements in a page.
javascript:(function(){s=document.getElementsByTagName('SCRIPT');tx='';sr=[];for(i=0;i<s.length;i++){with(s.item(i)){t=text;if(t){tx+=t;}else{sr.push(src)};}};with(window.open()){document.write('<textarea%20id="t">'+(sr.join("\n"))+"\n\n-----\n\n"+tx+'</textarea><script%20src="http://jsbeautifier.org/beautify.js"></script><script>with(document.getElementById("t")){value=js_beautify(value);with(style){width="99%";height="99%";borderStyle="none";}};</script>');document.close();}})();
actually more useful would be to visit the source site http://jsbeautifier.org/

Categories