I want to include files using shortcode, but problem is that preg_replace isn't working properly in for loop.my code is below:
<?php
$output = "[#include('file1')] [#include('file2')]";
if(preg_match_all("/\[\#include\(\'(.*?)\'\)\]/", $output, $match)){
for($i=0;$i<count($match);$i++){
$output = preg_replace("/\[\#include\(\'(.*?)\'\)\]/", $match[1][$i], $output);
}
}
echo $output;
the above code prints "file1 file1", it should be print "file1 file2" both file name but it is not printing the both files name.Please tell where i'm wrong.
What you're looking for is already in the output array of your regular expression (i.e. in the $match array), you just need to get it using implode()!
<?php
$output = "[#include('file1')] [#include('file2')]";
preg_match_all("/\[\#include\(\'(.*?)\'\)\]/", $output, $match);
echo $out = implode(' ', $match[1]);
?>
Related
I'm trying to create a simple PHP find and replace system by looking at all of the images in the HTML and add a simple bit of code at the start and end of the image source. The image source has something like this:
<img src="img/image-file.jpg">
and it should become into this:
<img src="{{media url="wysiwyg/image-file.jpg"}}"
The Find
="img/image-file1.jpg"
="img/file-2.png"
="img/image3.jpg"
Replace With
="{{media url="wysiwyg/image-file.jpg"}}"
="{{media url="wysiwyg/file-2.png"}}"
="{{media url="wysiwyg/image3.jpg"}}"
The solution is most likely simple yet from all of the research that I have done. It only works with one string not a variety of unpredictable strings.
Current Progress
$oldMessage = "img/";
$deletedFormat = '{{media url="wysiwyg/';
$str = file_get_contents('Content Slots/Compilied Code.html');
$str = str_replace("$oldMessage", "$deletedFormat",$str);
The bit I'm stuck at is find the " at the end of the source to add the end of the required code "}}"
I don't like to build regular expressions to parse HTML, but it seems that in this case, a regular expression will help you:
$reg = '/=["\']img\/([^"\']*)["\']/';
$src = ['="img/image-file1.jpg"', '="img/file-2.png"', '="img/image3.jpg"'];
foreach ($src as $s) {
$str = preg_replace($reg, '={{media url="wysiwyg/$1"}}', $s);
echo "$str\n";
}
Here you have an example on Ideone.
To make it works with your content:
$content = file_get_contents('Content Slots/Compilied Code.html');
$reg = '/=["\']img\/([^"\']*)["\']/';
$final = preg_replace($reg, '={{media url="wysiwyg/$1"}}', $content);
Here you have an example on Ideone.
In my opinion what you are doing is not the best way this can be done. I would use abstract template for this.
<?php
$content = file_get_contents('Content Slots/Compilied Code.html');
preg_match_all('/=\"img\/(.*?)\"/', $content, $matches);
$finds = $matches[1];
$abstract = '="{{media url="wysiwyg/{filename}"}}"';
$concretes = [];
foreach ($finds as $find) {
$concretes[] = str_replace("{filename}", $find, $abstract);
}
// $conretes[] will now have all matches formed properly...
Edit:
To return full html use this:
<?php
$content = file_get_contents('Content Slots/Compilied Code.html');
preg_match_all('/=\"img\/(.*)\"/', $content, $matches);
$finds = $matches[1];
$abstract = '="{{media url="wysiwyg/{filename}"}}"';
foreach ($finds as $find) {
$content = preg_replace('/=\"img\/(.*)\"/', str_replace("{filename}", $find, $abstract), $content, 1);
}
echo $content;
I have this code for example:
ob_start();
echo "hello there";
$output = ob_get_contents();
return $output;
When I run it, I get back:
hello there
But how can I get back
echo "hello there";
Is there a way to do this easily?
To output arbitrary text as-is you can close the PHP script and then reopen it. Anything between the closing and opening tags is output as-is
ob_start();
?>echo "hello there";
<?php
$output = ob_get_contents();
return $output;
ob_get_contents will return the echoed output, so you can't use it to show actual code.
To simply print out code, I would try this:
$code = file_get_contents('your_code.php');
echo "<pre>{$code}</pre>";
Also you can write you code separatle as text and echo it or eval (if you need execution).
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
result:
This is a $string with my $name in it.
This is a cup with my coffee in it.
By representing it as a string:
ob_start();
$str = <<<STR
echo "hello there";
STR;
echo $str;
$output = ob_get_contents();
return $output;
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;
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]*)&?/;
I'm using the Simple HTML DOM Parser to retrieve a specific div from a website. I remove the part of the div that I don't want by using explode(). I then want to explode the kept part into a new array, but for some reason it doesn't get indexed as intended.
Why doesn't my last row with "echo $content[0];" print "Overall" while "echo $content[5];" does, when Overall is the first string? How do I fix this?
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Pur');
$content = $html->find('div[id=contentHiscores]', 0)->plaintext;
echo $content;
echo "<br><br><br><br>";
$content = explode("SkillRankLevelXP", $content);
$content = $content[1];
echo $content;
echo "<br><br><br><br>";
$content = explode(" ", $content);
echo $content[0];
?>
Between SkillRankLevelXP and Overall, there are 6 spaces, though the browser only shows it as 1. Use the "View Source" menu and you'll see what I mean.
You can use some RegEx to replace 2 or more spaces with just 1 space, and I think that will get you closer to what you want.
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Pur');
$content = $html->find('div[id=contentHiscores]', 0)->plaintext;
$content=preg_replace('/ {2,}/', ' ', trim($content));
$content = explode('SkillRankLevelXP ', $content);
$content = $content[1];
$content = explode(' ', $content);
print_r($content);
?>