Print ">" on every line - php

How would I do to print a ">" before every line?
if ($quoteid) {
echo '
> '.$quote['message'].'
';
}
Currently It Looks like this:
> I would move Heaven and Hell and anything in between to get to you.
You wouldn't be safe anywhere if I was mad at you.
And that's not bull; that's truth. I've went up against people.
You could pull a gun on me and if I'm mad at you I'm coming forward.
You'd have to shoot me to stop me and if you don't kill me... you're stupid cause the next time you see me I will kill you.
I want it to look like this:
> I would move Heaven and Hell and anything in between to get to you.
> You wouldn't be safe anywhere if I was mad at you.
> And that's not bull; that's truth. I've went up against people.
> You could pull a gun on me and if I'm mad at you I'm coming forward.
> You'd have to shoot me to stop me and if you don't kill me... you're stupid cause the next time you see me I will kill you.

if ($quoteid) {
// Replace new line with new line + "> "
echo '>' . str_replace("\n", "\n> ", $quote['message']);
}

if ($quoteid) {
echo ' > '.str_replace("\n","\n > ",$quote['message'])';
}

use str_getcsv or explode to get the lines of your textblock and then print every line (like you already do).

try with str_replace
$bodytag = str_replace("\n", ">", $quote['message']);

echo str_replace(array("\r\n", "\n"), "\n> ", $quote['message']);

try this sinp
if ($quoteid) {
echo str_replace("\n", "\n> ", $quote['message']);
}

echo '>'.substr("\n", "\n>", $quote['message']);

Related

whitespace PHP link

I'm using PHP to send a string with $_GET to another webpage.
My main issue is when I try to see the whole string in my link just appear the first word "Im".
I'm pretty sure it is by double quotes but I'm not sure to do this.
<a href="edit.php?title=library&description=Im" going="" to="" the="" library="" tomorrow="">
I try to put this in my PHP code to replace "" character to '' but it doesn't work.
$length = strlen($_GET['description']);
$i=0;
do{
$_GET['description'][$i] = preg_replace('/["]/', '', $_GET['description][$i]);
$i++;
}while($i < $length);
My PHP link is the next:
echo "<a href=edit.php?title=$_GET[title]&description=$_GET[description]>";
If anyone knows what's going on, please help me.
Have you tried using the rawurlencode function?
// if $_GET["title"] = "Im going to the library tomorrow" ...
echo "<a href=edit.php?title=" . rawurlencode($_GET["title"]) . "&description=" . rawurlencode($_GET["description"]) . ">";
You may need to use rawurldecode when you receive this.

str_ireplace or preg_replace replaced break tag into \r\n

I have read this post that discuss about converting html break tag into a new line in php. Other people said it's work for them but something weird happened to me.
this is the code I use:
$breaks = array("<br />", "<br>", "<br/>");
$jawaban = str_ireplace($breaks, "
", $jawaban1);`
and this is the code they use :
$breaks = array("<br />", "<br>", "<br/>");
$text = str_ireplace($breaks, "\r\n", $text);
both insert "\r\n" into the text , why is this happening ?
screenshot:
if there's any previous post / PHP method let me know
EDIT : adding my code that echo the textbox
<-- THIS WONT WORK -->
$username = $_SESSION['username'];
$unsafenomorsoal = $_POST['nomorsoal'];
$unsafejawaban = $_POST['jawaban'];
$nomorsoal = mysqli_real_escape_string($konek,$unsafenomorsoal);
$jawabannotcut = substr($unsafejawaban,0,50000);
$unsafejawabanfirst = nl2br($jawabannotcut);
$jawaban1 = mysqli_real_escape_string($konek,$unsafejawabanfirst);
$breaks = array("<br />","<br>","<br/>");
$jawaban = str_ireplace($breaks, PHP_EOL, $jawaban1);
$_SESSION['textvaluejawaban'] = $jawaban;
and this is what echoed :
echo "<div class=\"head-main-recent-background\" style=\"background:white;width:99%;color:black;text-align:left;height:1000px;position:relative;top:130px;margin-top:10px;\">- Jawab Soal -<br/>".$jawabanerror."<br/>Nama : ".$_SESSION['username']."<br/>
<form method=\"post\" action=\"prosesjawabsoal.php\">
<input type=\"hidden\" name=\"nomorsoal\" value=\"".$_SESSION['nomorsoal']."\"/>
Jawaban : <br/>
<textarea placeholder=\"Max 40.000 Huruf\" style=\"overflow- x:none;width:99%;height:300px;\" type=\"text\" name=\"jawaban\" maxlength=\"40000\" >".$_SESSION['textvaluejawaban']."</textarea>
<br/>Captcha <br/>
<div style=\"overflow:hidden;\" class=\"g-recaptcha\" data- sitekey=\"6LfYQicTAAAAAFstkQsUDVgQ60x_93obnKAMKIM9\"></div><br/>
<button type=\"submit\" name=\"submit\" style=\"margin-top:10px;height:auto;width:auto;\">Kirim Jawaban</button>
</form>
</div>";
Note : The snippet won't work because it's php
Sorry i used snippet due to error while posting the code !
EDIT :
tried preg_replace() method but still same result
EDIT :
change title to tell that preg_replace not work
Your problem is the mysqli_real_escape_string(). The converts the "\r\n" into a string to make it safe to input into the database. Remove it completely. Instead use htmlspecialchars when you output to screen:
echo htmlspecialchars($myUnsafeVar);
Apply these rules (as a starting point, there's always possible exceptions, but in rare cases):
use mysqli_real_escape_string when inputting strings into a database. It won't do what you expect when outputting to screen - so anything that has been mysql escaped() should not appear on screen.
use htmlspecialchars (which you don't have!) when outputting to screen.
use url_encode for adding stuff into a URL
There are also many different "escape" function (e.g. inserting into JSON, inserting into mysql, inserting into other databases). Use the right one for what you need - and don't use it for other purposes.
Check the functions for more details.
As it currently stands your code is not safe even with all those efforts - but it's really simple to fix!
try with preg_replace() function and no need of \n\r both you can do with \n or PHP_EOL only
$jawaban = preg_replace('#<br\s*?/?>#i', "\n", $jawaban1);
or
$jawaban = preg_replace('#<br\s*?/?>#i', PHP_EOL, $jawaban1);
you must knowing these before working with strings:
"\n\r" means new line.
'\n\r' doesn't mean new line.
doesn't mean new line. It's just HTML number for HTML Symbols. when you are using it, you mean just show \n\r in your browser. this is answer to your question:
both insert "\r\n" into the text , why is this happening?
so, after knowing that, you understand:
if your $jawaban1 string is
Hello <br> and welcome!
and your code is
$breaks = array("<br />", "<br>", "<br/>");
$jawaban = str_ireplace($breaks, "
", $jawaban1);
It means, $jawaban will be exactly like this:
Hello
and welcome!
without any \n\r and just your browser showing it like this:
Hello \n\r and welcome!
If you want to replace all br by \n\r just use the code in your question:
$breaks = array("<br />", "<br>", "<br/>");
$text = str_ireplace($breaks, "\r\n", $text);
About preg_replace()
When you can use str_ireplace, Don't use preg_replace. str_ireplace is faster.
Don't do it if you don't need it
in your code you did this:
$unsafejawabanfirst = nl2br($jawabannotcut);
and right after that you want to replace br with \n\r. It's like do and undo. I see that you are trying to show it again inside textarea element. so don't replace \n\r with br. the solution? don't change \n\r at all and if you want save it to the db just save it with \r\r. when you need it to show outside of textarea element just use nl2br function.
There is always something that saves my day, it is actually a workaround and your question is a trigger for me to get deeper to this matter - once for all.
For now, here you go - nice & sleek workaround:
There is already nl2br() function that replaces inserts <br> tags before new line characters:
Example (codepad):
<?php
// Won't work
$desc = 'Line one\nline two';
// Should work
$desc2 = "Line one\nline two";
echo nl2br($desc);
echo '<br/>';
echo nl2br($desc2);
?>

How can I make a timer animation in the command line?

This is a little silly, but something I've wanted to do before and could never figure out. I have a PHP script that runs from the command line. I'd like a little timer animation to let the user know the script is still running. Here's what I have:
while (1 == 1) {
echo '—';
usleep(100000);
echo '\';
usleep(100000);
echo '|';
usleep(100000);
echo '/';
}
But how do I get each echo to replace the character before it?
You need to print a backspace before each character, for example:-
echo '-';
usleep(100000);
echo "\b/";
Note that you have to use double quotes here or the escape sequence won't work.
I'm sure you can work the rest out :)
If "\b" doesn't work try:-
echo chr(8) . '/';
Try the php ncurses extension:
http://php.net/manual/en/ref.ncurses.php
You can use
echo "yourCharacter1\r";
usleep(100000);
echo "yourCharacter1\r";
or
echo "\ryourCharacter1\r";
usleep(100000);
echo "\ryourCharacter2\r";
\r sends the cursor back to position 0 on the same line.
Also you may try (found online)
system("clear"); // before you echo new characters
or
passthru('clear'); // before you echo new characters

Replace " ’ " with " ' " in PHP

I'm grabbing a string from the database that could be something like String’s Title however I need to replace the ’ with a ' so that I can pass the string to an external API. I've used just about every variation of escaped strings in str_replace() that I can think of to no avail.
$stdin = mb_str_replace('’', '\'', $stdin);
Implementation of mb_str_replace() here: http://www.php.net/manual/en/ref.mbstring.php#107631
I mean this:
<?php
function mb_str_replace($needle, $replacement, $haystack) {
return implode($replacement, mb_split($needle, $haystack));
}
echo mb_str_replace('’', "'", "String’s Title");
It may solve encoding problems.
I have just tested this:
echo str_replace('’', "'", $cardnametitle);
//Outputs: String's Title
Edit: I believe that entries in your database have been htmlentitiesed.
Note: I'm pretty sure this is not a good solution, even though it did solve your problem I think there should be a better way to do it.
Try this
$s = "String’s Title";
$h = str_replace("’","'",$s);
echo $h;
Also can Try with preg_replace
echo preg_replace('/\’/',"'","String’s Title");
I don't know why str_replace() is not working for you.
I feel you haven't tried it in correct way.
Refer LIVE DEMO
<?php
$str = "String’s Title";
echo str_replace('’', '\'', $str) . "\n";
echo str_replace("’", "'", $str);
?>
OUTPUT:
String's Title
String's Title
UPDATE 1:
You may need to try setting the header as
header('Content-Type: text/html; charset=utf-8');
I came across a similar issue trying to replace apostrophes with underscores... I ended up having to write this (and this was for a WordPress site):
$replace = array(",","'","’"," ","’","–");
$downloadTitle = str_replace( $replace,"_",get_the_title($gallery_id));
I'm new to PHP myself, and realize this is pretty hideous code, but it worked for me. I realized it was the "’" that REALLY needed to be factored in for some reason.

preg_replace() in PHP - Replacing n-occurrences of a character with n-occurrences of another, when following a new line

I'm looking to replace all occurrences of space characters that follow a new line (or occur at the beginning of the input string). I know that I can achieve this using preg_replace_callback() with a callback that uses str_repeat and strlen, or similarly with the /e switch; but was wondering if it could be done more simply.
Currently I have the following:
$testData = " Hello\n to everybody\n in the world";
echo preg_replace('/^|\n( )+/', ' ', $pValue);
which gives:
" Hello to everybody in the world"
What I'm really after is:
" Hello\n to everybody\n in the world"
I should have searched harder before asking: found the answer (for a java solution) that seems to work perfectly. I'll leave the solution here for the sake of anybody else that has the same problem.
$testData = " Hello\n to everybody\n in the world";
echo preg_replace('/(?m)(?:^|\\G) /', ' ', $pValue);
Now just need to identify whether older versions of PHP support this.
You can use recursion
$pValue = " Hello\n to everybody\n in the world";
echo myReplace($pValue);
function myReplace($value)
{
$value = preg_replace('/(^|\\n)(( )*) /', '\1\2 ', $value);
if (preg_match('/(^|\\n)(( )*) /', $value))
{
$value = myReplace($value);
}
return $value;
}

Categories