Remove text from string php - php

I have a table called 'filename'. I try to output <a> tags in a loop from it like this:
<?php
while($sermon = mysql_fetch_assoc($sermonsQ)) {
echo '<a href="admin/'. $sermon ['filename'] . '">';
echo 'download</a></td>';
}
Current problem is, that $sermon['filename'] containts a leading path like path/test.mp3. But I need only the filename without the path, like test.mp3. How can I do this?

Use basename() for that. It will return the filename without the leading path:
basename($sermon ['filename'])

You can use path_info()
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
also mysql_* functions are depracated so you shouldn't use them for example to PDO or mysqli

<?php
while($sermon = mysql_fetch_assoc($sermonsQ)) {
$filename = explode('/',$sermon ['filename']);
echo '<a href="admin/'. $filename[1] . '">';
echo 'download</a></td>';
}

Related

PHP statement within echo

I am essentially trying to combine to PHP statements.
<?php echo ROOT_PATH; ?>
<?php echo file_get_contents( "../css/themes/subtitle.php"); ?>
I want to achieve something like this:
<?php echo file_get_contents( "ROOT_PATH/css/themes/subtitle.php"); ?>
If you call constants inside single or double quotes then it will be always picked as a string.
You need to add like below:
<?php echo file_get_contents( ROOT_PATH."/css/themes/subtitle.php"); ?>
Its pretty simple you can change it to following
//full path of the file
$file_name = ROOT_PATH."/css/themes/subtitle.php";
echo file_get_contents($file_name);
it should work
. operator is used to concatenating.
Like:
$str = "Hello";
echo $str;
echo "World";
can be written as
$str = "Hello";
echo $str."World";

Php echo url inside an echo

I am aware that I cannot use an echo inside an echo. My php code is:
echo '<img src="img/image1.jpg"
I want to use php variable as the source. Somethink like:
echo '<img src="php-code"
Using .(dot) you can concatenate php variable in echo statement.
echo '<img src="'.$src.'" />';
You have four options:
$url = '...';
//1
echo '<img src="' . $url . '">';
//2
echo "<img src='{$url}'>"; //Note that you have to use double quotes
//3
echo '<img src="';
echo $url;
echo '">';
//4
echo '<img src="', $url, '">'; //I would not recommend this one though
just skip first echo and write the html with an echo in the middle
<img src="<?=$src?>">
All of these works
echo '<img src="', $url, '">'; # This works by sending 3 different parameters to echo
echo '<img src="' . $url . '">'; # This works by concatenating 3 strings before echoing
echo '<img src="'; echo $url; echo '">'; # This works by echoing 3 strings in turn
echo "<img src=\"$url\">"; # All of these works by inserting
echo "<img src=\"${url}\">"; # the value of $url in the string
echo "<img src=\"{$url}\">"; # before echoing. " needs to be escaped.
# And finally, this (called HEREDOC) does the same thing as above
# only without ", so that sign is not needed to be escaped
echo <<<FOO
<img src="$url">
<img src="{$url}">
<img src="${url}">
FOO;
There are many solutions.
I prefer this one: <?php echo '<img src="'.$url.'">'; ?>
$url stands for the Image-Url, I guess you know ist.
You can do it also:
For example: <img src="<?php echo $url; ?>">
But I like the first method, it's a simple way to put out strings.

php string not working properly

My code in php is
echo $preference[0]."</br>";
echo '<p class="filter_entity">'.$preference[0].'<img src="' . base_url() . 'images/close-icon.png" onClick="filter_close()"></p>';
html out put is
Business
<img onclick="filter_close()" src="http://localhost/AEC/images/close-icon.png">
I want to put $preference[0] in filter_close() of my php code ... dont know how to do it .
Something like this?
$param = $preference[0];
echo '<p class="filter_entity">'.$preference[0].'<img src="' . base_url() . 'images/close-icon.png" onClick="filter_close(\''.echo $param.'\')"></p>';
Just append the variable as a parameter of the js function
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
echo "Some array item: {$array[0]}";

Can't send PHP $var=file_get_contents('path') variable to javascript function

This is my javascript function.
<script>
function output($file_name, $content)
{
document.getElementById("content_title").innerHTML=" "+$file_name;
document.getElementById("content").innerHTML=" "+$content;
}
</script>
This is my PHP code;
<?php
$dir = "img_png";
$files = scandir($dir);
$dir_length = count($files);
?>
This is the second part of my PHP code (with problem); Problem: When $content="any string"; everything works properly, but when $content=file_get_contents('file'); my triggered function doesn't change any .innerHTML elements at all.
<?php
for ($i=2;$i<$dir_length;$i++){
$title=explode(".png", $files[$i]);
$content=file_get_contents('./content_txt/tv.txt');
echo "<td><button id='button' class='button' onClick=\"output('", $title[0],"";
echo "', '", $content,"";
echo "')\"><img src=\"/img_png/", $files[$i], "\"></img></button></td>";
}
?>
Start solving your issue with this (use dots instead of commas to concatenate strings)
<?php
for ($i=2;$i<$dir_length;$i++){
$title=explode(".png", $files[$i]);
$content=file_get_contents('./content_txt/tv.txt');
echo "<td><button id='button' class='button' onClick=\"output('" . $title[0];
echo "', '" . $content;
echo "')\"><img src=\"/img_png/" . $files[$i] . "\"></img></button></td>";
}
?>
Also I would set $content-variable outside the loop and skip the end-tag (it doesn't make any difference because </img> is not used):
Also encode your data when sending from php (with urlencode)
<?php
$content=file_get_contents('./content_txt/tv.txt');
for ($i=2;$i<$dir_length;$i++){
$title=explode(".png", $files[$i]);
echo "<td><button id='button' class='button' onClick=\"output('" . $title[0];
echo "', '" . urlencode($content);
echo "')\"><img src=\"/img_png/" . $files[$i] . "\"></button></td>";
}
?>
In your javascript you have to decode the content:
function output($file_name, $content)
{
$content = decodeURI($content);
document.getElementById("content_title").innerHTML=" "+$file_name;
document.getElementById("content").innerHTML=" "+$content;
}
Your file contents may contain line breaks or special characters that need escaping in JS, you will need to escape special characters before passing the string to the javascript function.
"tvvvvvv";
I suppose you copy-paste that string from PHP to tv.txt
Remove " and ; - you don't need it in file.
And " is your problem in javascript.

Echo entire pre-compiled php page

For example if I had the script:
<?php
$page = "My Page";
echo "<title>" . $page . "</title>";
require_once('header.php');
require_once('content.php');
require_once('footer.php');
?>
Is there something I can add to the bottom of that page to show the entire pre-compiled php?
I want to literally echo the php code, and not compile it.
So in my browser I would see the following in code form...
// stuff from main php
$page = "My Page";
echo "<title>" . $page . "</title>";
// stuff from require_once('header.php');
$hello = "Welcome to my site!";
$name = "Bob";
echo "<div>" . $hello . " " . $name . "</div>";
// stuff from require_once('content.php');
echo "<div>Some kool content!!!!!</div>";
// stuff from require_once('footer.php');
$footerbox = "<div>Footer</div>";
echo $footerbox;
Is this possible?
There's no way to do it native to PHP, but you could try to hack it if you just wanted something extremely simplistic and non-robust:
<?php
$php = file_get_contents($_GET['file']);
$php = preg_replace_callback('#^\s*(?:require|include)(?:_once)?\((["\'])(?P<file>[^\\1]+)\\1\);\s*$#m', function($matches) {
$contents = file_get_contents($matches['file']);
return preg_replace('#<\?php(.+?)(?:\?>)?#s', '\\1', $contents);
}, $php);
echo '<pre>', htmlentities($php), '</pre>';
Notes:
Warning: Allowing arbitrary file parsing like I've done with the fist line is a security hole. Do your own authentication, path restricting, etc.
This is not recursive (though it wouldn't take much more work to make it so), so it won't handle included files within other included files and so on.
The regex matching is not robust, and very simplistic.
The included files are assumed to be statically named, within strings. Things like include($foo); or include(__DIR__ . '/foo.php'); will not work.
Disclaimer: Essentially, to do this right, you need to actually parse the PHP code. I only offer the above because it was an interesting problem and I was bored.
echo '$page = "My Page";';
echo 'echo "<title>" . $page . "</title>";';
echo file_get_contents('header.php');
echo file_get_contents('content.php');
echo file_get_contents('footer.php');
For clarity I'd put the title generation in it's own file, then just use a series of echo file_get_contents()...
echo file_get_contents('title.php');
echo file_get_contents('header.php');
echo file_get_contents('content.php');
echo file_get_contents('footer.php');

Categories