Protect Email with PHP - output is two words - php

This is my coding:
<?php
function protected_email($phpemail,$text='',$echo=true)
{
$pieces = explode("#", $phpemail);
$return = '
<script type="text/javascript">
var a = "<a href=\'mailto:";
var b = "' . $pieces[0] . '";
var c = "' . $pieces[1] .'";
var d = "\' class=\'email\'>";
var e = "</a>";';
if($text != ''){
$return .= 'document.write(a+b+"#"+c+d+\''.$text.'\'+e);';
}else{
$return .= 'document.write(a+b+"#"+c+d+b+"#"+c+e);';
}
$return .= '
</script>
<noscript>Please enable JavaScript to view emails</noscript>';
if($echo == true)
{
echo $return;
}else{
return $return;
}
}
?>
<?php echo protected_email('change#email.com', Their Title); ?>
I want to display the person's title instead of their email address, but I cannot get both words to show in their title, unless I remove the space between. How do I get it to dsiplay both words?
Display this:
Their Title
Rather display than:
change#email.com
I'm sure it is something really simple, but I am at a mental block right now.

Related

PHP if statement not working in preg_replace

I have a simple PHP of statement but it keeps giving me 'Internal 500'.
Can anyone see what is wrong with this code?
(It works without the 'if')
$fullyfiltered = preg_replace('/<span>(.*?)<\/span>/', '<div class="chat-message ' if('$1'=="MichaelD"){'me'}else{'chat-midnightblue'}'"><div class="chat-contact"><img src="/assets/demo/avatar/tswan.png" alt=""></div><div id="chat-text" class="chat-text">$1: ', $nearlyfiltered);
EDIT - Full script:
<script>
setInterval(function(){
document.getElementById('chat-text').innerHTML = '';
<?php
$fh = fopen('chat.txt','r');
while ($line = fgets($fh)) {
//echo "<p>" . $line . "</p>";
$filtered = str_replace("'", "\\'", $line);
$almostfiltered = str_replace("<span></span>\n", "", $filtered);
$nearlyfiltered = trim(preg_replace('/\s\s+/', ' ', $almostfiltered));
$fullyfiltered = preg_replace('/<span>(.*?)<\/span>/', '<div class="chat-message ' if('$one'=="MichaelD"){'me'}else{'chat-midnightblue'}'"><div class="chat-contact"><img src="/assets/demo/avatar/tswan.png" alt=""></div><div id="chat-text" class="chat-text">$1: ', $nearlyfiltered);
if(!empty($fullyfiltered)){
$endingp = "</div></div>';";
} else {
$endingp = "';";
}
echo "document.getElementById('chat-text').innerHTML = document.getElementById('chat-text').innerHTML + '" . $fullyfiltered . $endingp;
}
fclose($fh);
?>
},5000);
</script>
callback must be a function, not just random script parts. Please read the manual (http://php.net/manual/en/function.preg-replace-callback.php)

PHP echo text then sleep 10 seconds then echo another text

I've tried solutions from below links. But none of them give luck.
php-output-text-before-sleep
php-output-data-before-and-after-sleep
php-time-delay-using-ob-flush-with-loading-message
Actually below is my script.
<?php
include 'ini/INI.class.php';
$CompIP = $_SERVER['REMOTE_ADDR'];
$inidata = (parse_ini_file("guard.ini",true));
$atm = time()-$inidata["guard"][$CompIP];
if ($atm>60) { $atm = 1; }
echo "<p>You will be redirected to report page in <span id='counter'>" . $atm . "</span> second(s).</p>";
sleep($atm);
//my
//100
//line
//user report from mysql
$ini = new INI('guard.ini');
$ini->data['guard'][$CompIP] = time();
$ini->write();
?>
Still I get the whole content include 'You will be redirected to .......' after $atm (pause seconds) seconds.
My Workaround
<?php
include 'ini/INI.class.php';
$CompIP = $_SERVER['REMOTE_ADDR'];
$inidata = (parse_ini_file("guard.ini",true));
$atm = (time()-(isset($inidata["guard"][$CompIP]) ? $inidata["guard"][$CompIP] : 0));
if ($atm<60)
{
echo "<p>You will be redirected to report page in <span id='counter'>" . (60-$atm) . "</span> second(s).</p> <script type='text/javascript'> function countdown() { var j = document.getElementById('counter'); j.innerHTML = parseInt(j.innerHTML)-1; if (parseInt(j.innerHTML)<=0) { j.innerHTML = 0; location.href = 'tr.php'; } } for (i=1;i<=" . (60-$atm) . ";i++) { setTimeout(function(){ countdown(); },i*1000); } </script>";
} else { mysqlreport; $ini = new INI('guard.ini');
$ini->data['guard'][$CompIP] = time();
$ini->write();
}
?>
You can use JavaScript for this purpose and pass variables from PHP to JavaScript simply by writing the JavaScript code inside "echo". I think something like this will do the trick.
For redirection, the below example will give you an idea:
<?php
$url = "http://google.com";
$step = "1000";
$start = 12;
echo 'Redirection After <h1 id="counter">'.$start.'</h1> ';
echo '
<script>
var x = '.$start.';
setInterval(function(){
if(x==1){
window.location = "'.$url.'";
}
document.getElementById("counter").innerHTML = x;
x--;
}, "'.$step.'");
</script>';
?>
As for your content that you want to output, just place a tag and with JavaScript. Also, you can update it every 10 seconds; the technique is this, how you do it is up to you.

Split text from the database in two columns

I have a bootstrap based website with 2 columns(col-lg-6 and col-lg-6).
I have the ability to add text from an admin panel (articles).
How can I make it so it splits in the second div (col-lg-6) after a specific number of characters or after I insert a character or something that will represent the breakage?
I prefer a PHP solution, but Javascript/Jquery would do too
edit *
if(isset($_GET['chapter']) && !empty($_GET['chapter'])){
$chapterid = (int)$_GET['chapter'];
$chaptertitle = mysql_query("SELECT bookname FROM books WHERE id=$chapterid");
$chaptertitle = mysql_fetch_array($chaptertitle);
$chaptertitle = $chaptertitle[0];
$chapter = mysql_query("SELECT bookContent FROM books WHERE id=$chapterid");
$chapter = mysql_fetch_array($chapter);
$chapter = $chapter[0];
$bookcontent = mysql_query("SELECT * FROM books WHERE bookid=$id");
$bookcontainer = '';
while($row = mysql_fetch_array($bookcontent)){
$bookcontainer .= '<h2>'.$row['bookname'].'</h2>' . $row['bookContent'];
}
and this is the place where the code is printed
<div class="row">
<div class="col-lg-6" id="paragraphs">
<div style="width:100%;">
<?php
//if($errors == false){
echo $chapter;
//}
//else{
//echo 'erorr madarfaker';
//}
?>
</div>
</div>
<div class="col-lg-6"></div>
</div>
If anyone is interested in this: I made 2 fields in the admin panel and the variable that was being sent to the database was "field 1 + ' | ' + field 2" and then I used explode to display the content on 2 sides, separator being " | "
Choose an arbitrary character for your split, like "|". Based on your posted code, use this as your echo statement:
echo str_replace($chapter, "|", "</div></div><div class='col-lg-6' id='paragraphs'><div style='width:100%;'>");
I am not sure if this solves your problem, but you can try :
<?php
// An array with your database data :
$arr = array('val1', 'val2', 'val3', 'val4');
$col1 = "";
$col2 = "";
$flag=false;
for($i=0; $i<sizeof($arr); $i++){
//Break condition :
$flag = ($arr[$i] == "val3" || $flag != true ) ? true : false;
if(!$flag){
$col1 .= $arr[$i];
}
else{
$col2 .= $arr[$i];
}
}
$html = "";
if($col1 != ""){
$html .= "<div class='col-lg-6'>".$col1."</div>";
}
if($col2 != ""){
$html .= "<div class='col-lg-6'>".$col2."</div>";
}
// The two div with your content (if the break rule is found) :
echo $html;
?>
If anyone is interested in this: I made 2 fields in the admin panel and the variable that was being sent to the database was "field 1 + ' | ' + field 2" and then I used explode to display the content on 2 sides, separator being " | "

PHP Search: Using Jquery to alter a php a value

I have a comics website. A feature it has is to allow users to search comics... the search will instantly parse the input and return thumbnail results based on matching title and keywords.
Originally, the search would return all of the results, and the bounding search box would expand infinitely downward, holding every single comic result. I thought it may be a nice touch to limit the results to 4, and display a message like "load 5 remaining images" if the user chooses to do so.
If they click on that message, I wanted limiting php variable to be removed or changed.
So far, it loads with the limit, and shows a link...
EDIT: Latest Code:
search_field.php (the search file that get's included on a page... this file calls search.php via JQuery):
<?php $site = (isset($_GET['site']) ? ($_GET['site']) : null); ?>
<div id="sidebar" class="searchborder">
<!--Allow users to search for comic-->
<!--<span class="search">Search for <?php// echo (($site == "artwork") ? 'artwork' : 'a comic'); ?> </span>-->
<script type="text/javascript">
function GetSearch(mySearchString){
$.get("./scripts/search.php", {_input : mySearchString, _site : '<?php echo $site ?>'},
function(returned_data) {
$("#output").html(returned_data);
}
);
}
</script>
<center>
<table>
<tr>
<td>
<span class="search">
<img src="./images/SiteDesign/Search.png" />
<input type="text" onkeyup="GetSearch(this.value)" name="input" value="" />
<!--<input id="site" type="hidden" value="<?php// echo $site; ?>">-->
</span>
</td>
</tr>
</table>
</center>
<span id="output"> </span>
</div>
search.php, the file that's called to parse the string and return the results:
<?php
//Query all images:
include 'dbconnect.php';
$site = $_GET['_site'];
$input = (isset($_GET['_input']) ? ($_GET['_input']) : 0);
$siteChoice = (isset($_GET['_choice']) ? ($_GET['_choice']) : $site);
$start = (isset($_GET['_start']) ? ($_GET['_start']) : null);
echo "start: " . $start;
//if user goes to hittingtreeswithsticks.com... no "site" value will be set... so I need to set one
if ($site == null) {
$site = "comics";
}
if ($siteChoice == "artwork") {
$sql = "SELECT id, title, keywords, thumb FROM artwork";
$thumbpath = "./images/Artwork/ArtThumbnails/";
}
else if ($siteChoice == "comics") {
$sql = "SELECT id, title, keywords, thumb FROM comics";
$thumbpath = "./images/Comics/ComicThumbnails/";
}
else {
$sql = "SELECT id, title, keywords, thumb FROM $site";
if ($site == "artwork") {
$thumbpath = "./images/Artwork/ArtThumbnails/";
}
else {
$thumbpath = "./images/Comics/ComicThumbnails/";
}
}
/* For this to work, need all comics replicated in an "All Comics" file along with "All Thumbnails"
else {
$sql = "SELECT id, title, thumb FROM comics
UNION
SELECT id, title, thumb FROM artwork";
$thumbpath = "./images/AllThumbnails/";
}*/
$imgpaths = $mysqli->query($sql);
mysqli_close($mysqli);
$idresult = array();
$imgresult = array();
$thumbresult = array();
//CHECK IF $INPUT == IMAGE PATH
if (strlen($input) > 0)
{
while ($row = $imgpaths->fetch_assoc())
{
//query against key words, not the image title (no one will remember the title)
if (stripos($row['keywords'], $input) !== false || strtolower($input)==strtolower(substr($row['title'],0,strlen($input))))
//if (strtolower($input)==strtolower(substr($row['title'],0,strlen($input))))
{
array_push($idresult, $row['id']);
array_push($imgresult, $row['title']);
array_push($thumbresult, $row['thumb']);
}
}
//ECHO RESULTS ARRAY
if(count($imgresult) == 0)
{
echo "<p>no suggestions</p>";
}
else
{
echo "<ul>";
$k = 0;
$max = 4;
if (count($imgresult) > $max) {
while ($k < count($imgresult) && $k < $max)
{
echo '<li>
<span class="sidebarimages"><a href=".?action=viewimage&site=' . $siteChoice . '&id=' . $idresult[$k] . '">
<img src="./scripts/thumber.php?img=.'.$thumbpath.$thumbresult[$k].'&mw=90&mh=90"/></a></span>
</li>';
$k++;
}
$difference = count($imgresult)-$k;
echo "<br/><i><a href='.?action=homepage&site=" . $siteChoice . "&start=4' class='loadSearch'>load " . $difference . " more result" . (($difference != 1) ? 's' : '') . "... </a></i>";
}
else {
while ($k < count($imgresult))
{
echo '<li>
<span class="sidebarimages"><a href=".?action=viewimage&site=' . $siteChoice . '&id=' . $idresult[$k] . '">
<img src="./scripts/thumber.php?img=.'.$thumbpath.$thumbresult[$k].'&mw=90&mh=90"/></a></span>
</li>';
$k++;
}
}
echo "</ul>";
}
}
?>
<script type="text/javascript">
$(".loadSearch").click(function() {
//alert("Test");
$.get("./search.php", {_start : 4},
function (returned_data) {
$("#moreResults").html(returned_data);
}
);
});
</script>
Try this:
<script type="text/javascript">
$("#loadSearch").click(function() {
$.get('URL WITH QUERY', function(data) {
$('#results').html(data);
});
});
</script>
From what i get all you need is when "load more" is clicked only new results should be shown.
Load more has to be a url same as your search url.
Search/Autocomplete URL - example.com/autocomplete?q=xkd
Load More URL - example.com/autocomplete?q=xkd&start=4&max=1000
Just add two parameters to your url. start and max. Pass them to your queries and you get exact result.
Only verify Start < Max and are integers intval() and not 0 empty(). Also if Max <= 4 then dont show load more.
I would give all of your results back, then try to determine your results. If more then 4, loop out the first 4 results. If the user clicks on the load more button your start looping from your 4th element. That way you only need to hit the server once (per search).
Try to give back your results in json, so you can format it the way you like in your html file.
In pseudo code:
searchTerm = 'hello';
resultsFromServer = getResults($searchterm);
resultcounter = count(resultsFromServer);
if(resultcounter > 4)
loop 4 results
else
loop all results
$(".loadSearch").click(function(e) {
//alert("Test");
e.preventDefault();
$.get("./search.php", {_start : 4},
function (returned_data) {
$("#moreResults").html(returned_data);
}
);
I ended up going with jquery show and hide functions.
PHP Snippet:
//ECHO RESULTS ARRAY
if(count($imgresult) == 0)
{
echo "<p>no suggestions</p>";
}
else
{
echo "<ul>";
$k = 0;
$max = 4;
while ($k < count($imgresult) && $k < $max)
{
echo '<li>
<span class="sidebarimages"><a href=".?action=viewimage&site=' . $siteChoice . '&id=' . $idresult[$k] . '">
<img src="./scripts/thumber.php?img=.'.$thumbpath.$thumbresult[$k].'&mw=90&mh=90"/></a></span>
</li>';
$k++;
}
$difference = count($imgresult)-$k;
echo '<div id="moreResults">';
while ($k < count($imgresult))
{
echo '<li>
<span class="sidebarimages"><a href=".?action=viewimage&site=' . $siteChoice . '&id=' . $idresult[$k] . '">
<img src="./scripts/thumber.php?img=.'.$thumbpath.$thumbresult[$k].'&mw=90&mh=90"/></a></span>
</li>';
$k++;
}
echo '</div>';
if (count($imgresult) > $max) {
?>
<br />Load <?php echo $difference; ?> more result<?php echo (($difference != 1) ? 's' : ''); ?>...
<?php
}
echo "</ul>";
}
}
Jquery:
<script type="text/javascript">
$("#moreResults").hide();
$("#showMore").click(function() {
$("#moreResults").show();
$("#showMore").hide();
});

php outputs part of code without any echo

Here's my piece of code(full body code):
<body>
<script type='text/javascript'>
function AddEvent(Syear, Smonth, Sday, Eyear, Emonth, Eday, hallNumber){
...
}
</script>
<?php
function GetMonthByCoding($first , $second , $third) {
...
}
function GetDateByCoding($coding){
...
}
function GetDateFromLine($line){
...
}
$userid = '...';
$magicCookie = 'cookie';
$feedURL = "...";
$sxml = simplexml_load_file($feedURL);
foreach ($sxml->entry as $entry) {
$title = stripslashes($entry->title);
if ($title == "HALL") {
$summary = stripslashes($entry->summary);
$date = GetDateFromLine($summary);
echo ("<script type='text/javascript' language='JavaScript'> AddEvent(" . $date['start']['year'] . ", " . $date['start']['month'] . ", " . $date['start']['day'] . ", " . $date['end']['year'] . ", " . $date['end']['month'] . ", " . $date['end']['day'] . "); </script>");
}
}
?>
</body>
AddEvent() is JavaScript function defined earlier.
What I get in my browser is:
entry as $entry) { $title = stripslashes($entry->title); if ($title == "HALL") { $summary = stripslashes($entry->summary); $date = GetDateFromLine($summary); echo (""); } } ?>
Looks like it was an echo but as you can see there is no echo right in the middle of foreach.
Can anyone say what I am doing wrong?
PHP is not installed, or it is not enabled, or the file is not a .php file or the server has not been told to recognise it as a file to parse.
Try View Source and you should see all your PHP code. The only reason part of it shows up is because everything from <?php to the first > is considered by the browser to be an invalid tag.
I found the problem, it was in the name of variable sxml. I renamed it and the problem escaped.

Categories