Auto line break after 5 characters - php

Guys i just wanna ask if there's a possibility: for example $currenttext = "someexamplepls";
if you would like to show or print it just use print "$currenttext"; then the prepared output look like this someexamplepls. My question is if i would like to print it like this:
somee
xampl
epls
in every 5 character there is automatic newline what should I do?

Use wordwrap():
echo wordwrap($str, 5, "\n", true);
In case you want HTML linebreaks use <br> instead of \n.

Related

PHP str_replace <br>, \r\n into Real New Line in MySQL [duplicate]

This question already has answers here:
Converting <br /> into a new line for use in a text area
(6 answers)
Closed 5 years ago.
I have a text with <br> tags and I want to save it into MySQL database as a new line. not HTML tags.
for example :
$string = 'some text with<br>tags here.'
and I want to save it into MySQL like this :
some text with
tags here
what right str_replace for this purpose? thank you.
There is already a function in PHP that converts a new line to a br called nl2br(). However, the reverse is not true. Instead you can create your own function like this:
function br2nl($string)
{
$breaks = array("<br />","<br>","<br/>");
return str_ireplace($breaks, "\r\n", $string);
}
Then whenever you want to use it, just call it as follows:
$original_string = 'some text with<br>tags here.';
$good_string = br2nl($original_string);
There are three things worth mentioning:
It may be better to store the data in the database exactly as the user entered it and then do the conversion when you retrieve it. Of course this depends what you are doing.
Some systems such as Windows use \r\n. Some systems such as Linux and Mac use \n. Some systems such as older Mac systems user \r for new line characters. Given this and especially if you choose to use point 1. above, you might prefer to use the PHP constant PHP_EOL instead of \r\n. This will give the correct new line character no matter what system you are on.
The method I posted above will be more efficient than preg_replace. However, it does not take into account non-standard HTML such as <br /> and other variations. If you need to take into account these variations then you should use the preg_replace() function. With that said, one can overthink all the possible variations and yet still not account for them all. For example, consider <br id="mybreak"> and many other combinations of attributes and white space.
You could use str_replace, as you suggest.
$string = 'some text with<br>tags here.';
$string = str_replace('<br>', "\r\n", $string);
Although, if your <br> tags may also be closed, <br /> or <br/>, it may be worth considering using preg_replace.
$string = 'some text with<br>tags here.';
$string = preg_replace('/<br(\s+\/)?>/', "\r\n", $string);
Here try this. This will replace all <br> to \r\n.
$string = 'some text with<br>tags here.';
str_replace("<br>","\r\n",$string);
echo $string;
Output:
some text with
tags here.
You can use htmlentities— Convert all HTML characters to entities and html_entity_decode to Convert HTML entities to characters
$string = 'some text with<br>tags here'
$a = htmlentities($string);
$b = html_entity_decode($a);
echo $a; // some text with<br>tags here
echo $b; // some text with<br>tags here
Try :
mysql_real_escape_string
function safe($value){
return mysql_real_escape_string($value);
}

Substr String With HTML In PHP

I have some text that comes back from my database like so:
<span rgb(61,="" 36,="" 36);="" font-family:="" 'frutiger="" neue="" w01="" book',="" 'helvetica="" neue',="" helvetica,="" arial,="" sans-serif;="" line-height:="" 23.8px;"="">The Department of ...
I use echo html_entity_decode($item->body); to display:
The Department of ...
However, if I use the PHP substr function on this content it never displays correctly. It will display the first x characters of HTML and not the HTML formatted text.
Here's what I tried: echo substr(html_entity_decode($item->body), 0, 5);
But it doesn't display anything. If I try an amount like 0, 200); it will display:
The Department of Molec
But this is most definitely not the first 200 characters of the formatted text because the first character is T.
My idea is that there must be way to format and then substr, even though I can't get it to work using html_entity_decode() and substr() by themselves.
Can anybody help me out here? Thanks!
Try to use this instead of html_entity_decode():
strip_tags($item->body);
strip_tags removes all HTML tags from the string. So you better of treating the string and then do something with it.
You will see the output in the source code, but it is not beeing rendered. The source code will show:
echo substr(html_entity_decode($item->body), 0, 5);
// Output: "<span"
What you probably want to do is search for the end of the html-tag, and display 5 characters after that, like:
$text = html_entity_decode($item->body);
$start = strpos( $text, '>' ) + 1;
echo substr( $text, $start, 5 );

PHP replace : find and replace the same characters with different text

How can I find and replace the same characters in a string with two different characters? I.E. The first occurrence with one character, and the second one with another character, for the entire string in one go?
This is what I'm trying to do (so users need not type html in the body): I've used preg_replace here, but I'll willing to use anything else.
$str = $str = '>>Hello, this is code>> Here is some text >>This is more code>>';
$str = preg_replace('#[>>]+#','[code]',$str);
echo $str;
//output from the above
//[code]Hello, this is code[code] Here is some text [code]This is more code[code]
//expected output
//[code]Hello, this is code[/code] Here is some text [code]This is more code[/code]
But problem here is, both >> get replaced with [code]. Is it possible to somehow replace the first >> with [code] and the second >> with a [/code] for the entire output?
Does php have something to do this in one go? How can this be done?
$str = '>>Hello, this is code>> Here is some text >>This is more code>>';
echo preg_replace( "#>>([^>]+)>>#", "[code]$1[/code]", $str );
The above will fail if something like the following is your input:
>>Here is code >to break >stuff>>
To deal with this, use negative lookahead:
#>>((?!>[^>]).+?)>>#
will be your pattern.
echo preg_replace( "#>>((?!>[^>]).+?)>>#", "[code]$1[/code]", $str );

Keep all html whitespaces in php mysql

i want to know how to keep all whitespaces of a text area in php (for send to database), and then echo then back later. I want to do it like stackoverflow does, for codes, which is the best approach?
For now i using this:
$text = str_replace(' ', '&nbs p;', $text);
It keeps the ' ' whitespaces but i won't have tested it with mysql_real_escape and other "inject prevent" methods together.
For better understanding, i want to echo later from db something like:
function jack(){
var x = "blablabla";
}
Thanks for your time.
Code Blocks
If you're trying to just recreate code blocks like:
function test($param){
return TRUE;
}
Then you should be using <pre></pre> tags in your html:
<pre>
function test($param){
return TRUE;
}
</pre>
As plain html will only show one space even if multiple spaces/newlines/tabs are present. Inside of pre tags spaces will be shown as is.
At the moment your html will look something like this:
function test($param){
return TRUE;
}
Which I would suggest isn't desirable...
Escaping
When you use mysql_real_escape you will convert newlines to plain text \n or \r\n. This means that your code would output something like:
function test($param){\n return TRUE;\n}
OR
<pre>function test($param){\n return TRUE;\n}</pre>
To get around this you have to replace the \n or \r\n strings to newline characters.
Assuming that you're going to use pre tags:
echo preg_replace('#(\\\r\\\n|\\\n)#', "\n", $escapedString);
If you want to switch to html line breaks instead you'd have to switch "\n" to <br />. If this were the case you'd also want to switch out space characters with - I suggest using the pre tags.
try this, works excellently
$string = nl2br(str_replace(" ", " ", $string));
echo "$string";

ECHO Variable include newline

I have the following code:
<p><?php echo $item['desc']; ?></p>
The code pulls the following from the database:
Point 1
Point 2
Point 3
and displays it as: Point 1 Point 2 Point 3,
What do I need to do to get the new lines included, I've tried adding /n or tags into to the DB reference however it is not making any difference.
This should work
<p><?php echo nl2br($item['desc']); ?></p>
Otherwise:-
echo nl2br(str_replace(' ',"\n", $item['desc']));
The newline is included. It's just not displayed in the browser, because the HTML standard says so. If you want it displayed in the browser, change newline to <br> tags, e.g. using nl2br()
Maybe the newlines are being printed, but you need them to be <br> tags, so they will appear as newlines on the webpage? You can use the function nl2br() for that:
<p><?php echo nl2br($item['desc']); ?></p>
Try this
echo str_replace(' ',"\n", $item['desc']);
\n won't work because the string you print has to be processed by the browser in html.
Use <br> tag instead.
Try like
echo str_replace(' ',"<br>", $item['desc']);
I couldn't understand exactly what you want but probably it's something like this:
$string = "Point 1 Point 2 Point 3";
$string = preg_replace("/(point \d+ )/i", "\$1\r\n", $string);
echo $string
Output:
Point 1
Point 2
Point 3
Note: Use only nl2br (see other answers) if the new lines are already in the string but just not displayed.
if your code in one line like TEST TEST TEST you can use preg_replace function :
echo preg_replace("{[ ]+}","<br/>",$items['desc']);
if your string like this TEST 1 TEST 2 ... and your goal is :
TEST 1
TEST 2
... so on..
you can use this pattern:
$t = preg_match_all("{[\w]+[ ]?[0-9]+}",$items['desc'],$m);
foreach ($m[0] as $val) { echo $val."<br/>"; }

Categories