How to wordwrap to 3 lines? - php

HI guys i want to know how to word wrap to 3 lines this is my code
$string = "this is a long string that i want condensed isjdjfibfhkbfhdbfsdbfsdhfbdsfhsbdhfkhvbsdhvshfsdjkvfjhjfdsffsdfsdfdsfdshfbdkhsvfvdfdhfsdvf";
echo wordwrap($string,55,"<br />\n",TRUE);

.wordwrap {
word-wrap: break-word;
}

You could put the text in a div or other block element that has style overflow = scroll.

Related

CSS (/ Wordpress) White Space property behaving strangely

Despite setting the "white-space" property to my paragraphs to "normal" in CSS, the breaks in some of my lines are spacing really strangely. Does anyone have an idea at to what may be the cause? Here is an example of this.
Thanks a lot
these lines are prob. seperated by different p elements.
word-break: break-all;
word-break: break-word;
word-break: keep-all;
overflow-wrap: break-word;
hyphens: manual;
hyphens: auto;
line-break: loose;
line-break: strict;
line-break: anywhere;
text-wrap: balance;
these are some styles can apply to change break points in text.
Upon investigation, it turns out my wordpress had been adding in random &nbsp breaks to my <p> elements. I found a solution to this in another thread which involves adding the following code to my functions.php file:
function replace_content($content) {
$content = htmlentities($content, null, 'utf-8');
$content = str_replace(" ", " ", $content);
$content = html_entity_decode($content);
return $content;
}
add_filter('the_content','replace_content', 999999999);
[Credits to 'Bruno' https://wordpress.stackexchange.com/questions/29702/wordpress-automatically-adding-nbsp]

preg_replace a hashtag that doesn't end with ;

I am currently using preg_replace to replace hashtags mentioned with html links like shown below. The issue is there is a possibility there will be html code as well being checked. So some css such as color: #000000; will force it to try convert that hex code into a link.
I basically need my regex to ignore doing any preg_replace if the last letter of a word is ;. Here's what I currently have:
$str = preg_replace('/#([a-zA-Z0-9!_%]+)/', '#$1', $str);
Example input: 'I like #action movies!'
Expected output: I like #action movies!'
I cannot use the end of the string to check this as chunks of text is checked at any given time so the string supplied could be #computer text text text #computer for instance.
Appreciate any assistance.
In your regex you can check if next to your hashtag there is a ;, non alphanumeric, end of line or end of string:
/#([a-zA-Z0-9!_%]+)([^;\w]{1}|$)/
Then use $1 and $2 accordingly
'#$1$2'
Your code will look like
$str = preg_replace('/#([a-zA-Z0-9!_%]+)([^;\w]{1}|$)/', '#$1$2',$str);
Here you can see some tests: https://regex101.com/r/yN4tJ6/65
Until a regEx guru come to your rescue (if ever...) and because you are in PHP; here is a solution with few lines of code.
$str="hi #def; #abc #ghi"; // just a test case (first one need be skipped)
if (preg_match_all('/#([a-zA-Z0-9!_%]+.?)/', $str,$m)){
foreach($m[1] as $k) if(substr($k,-1)!=';') {
$k=trim($k);
$str=str_replace("#$k","<a href='http://wxample.com/tags/$k'>#$k</a>",$str);
}
}
print "$str\n";
you can add a condition to check last string is ; or not and use it accordingly .
Example :
if (substr($str, -1)==';'){
//do nothing
}
else {
$str = preg_replace('/#([a-zA-Z0-9!_%]+)/', '#$1', $str);
}
Hope this help .
This regex should work:
#([\w!%]+(?=[\s,!?.\n]|$))
Demo: https://regex101.com/r/KrRiD3/2
Your PHP code:
$str = 'I like #strategy games #f1f1f1; #e2e2e2; #action games!';
$str = preg_replace('/#([\w!%]+(?=[\s,!?.\n]|$))/', '#$1', $str);
echo $str;
output:
I like #strategy games #f1f1f1; #e2e2e2; #action games!
Well, You can use below code, Actually I am new to regex so it is not that professional but it works, here it is
$data = "<p style='color:#00000;'>Heloo</p> #computer text text text #computer #say #goo1d #sd! #say_hello";
echo preg_replace("/(?<!\:)(\s+)\#([\w]+)(?!\;)/",'#$2',$data);
This expression I have use
/(?<!\:)(\s+)\#([\w]+)(?!\;)/
Output is
<p style='color:#00000;'>Heloo</p> #computer text text text #computer #say #goo1d #sd! #say_hello
I hope it helps someone.

PHP x lines than stop

How can i only echo 5 lines?
<?php
$text = Here i have a long long long text about 20 lines;
echo $text;
?>
So and i want something like that.->
TEXTLINE 1
TEXTLINE 2
TEXTLINE 3
TEXTLINE 4
TEXTLINE 5
And than stop.
Code for the mentioned text of Harri (at least this would be my approach):
$strings = explode(" ", $text);
for($i=0; $i<$your_lines; $i++) {
echo $strings[$i] . " ";
}
Explode the string to array, then loop through the array until last line you want to print. Printh each line while looping the array.
when you need lines (not words) use
$lines = explode(PHP_EOL, $text);
Assuming the output is a web browser, then this is a display issue, since what you are referring to as "line" depends on the width/height of the container. (It's not very clear what you try to ask)
You can set a width and height on a div using css and use overflow hidden on that to achieve a result like the one you want
demo
http://jsfiddle.net/g5Y5c/
.mydiv {width:100px;height:100px;overflow:hidden;}

nested bb code quotes how to>

Hi im using a pretty basic bbcode parser.
could you guys help me with a problem of mine?
but when for example this is written:
[quote=tanab][quote=1][code]a img{
text-decoration: none;
}[/code][/quote][/quote]
the output is this:
tanab said:
[quote=1]
a img{
text-decoration: none;
}
[/quote]
how would i go and fix that? im realllly bad at the whole preg_replace stuff.
this is my parser:
function bbcode($input){
$input = htmlentities($input);
$search = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[url=(.*?)\](.*?)\[\/url\]/is',
'/\[code\](.*?)\[\/code\]/is',
'/\[\*\](.*?)/is',
'/\\t(.*?)/is',
'/\[quote=(.*?)\](.*?)\[\/quote\]/is',
);
$replace = array(
'<b>$1</b>',
'<i>$1</i>',
'<img src="$1">',
'$2',
'<div class="code">$1</div>',
'<ul><li>$1</li></ul>',
' ',
'<div class="quote"><div class="quote-writer">$1 said:</div><div class="quote-body">$2</div></div>',
);
return preg_replace($search,$replace,$input);
}
This could be adapted with a recursive regex:
'/\[quote=(.*?)\](((?R)|.*?)+)\[\/quote\]/is'
Which will at least ensure that the output divs will not be incorrectly nested. But you would still have to run the regex twice or three times to catch all quote blocks.
Otherwise it would require a rewrite of your code with preg_replace_callback. Which I cannot be bothered to showcase, since this came up a few dozen times already (try the site search!), has been solved before, etc.

How to break a text into the next line in php?

i have this project which is blog type where I pull posts submitted from the server. The problem is that when the post is echo(ed) the text goes all in one line , how do I make it go to the next line after certain length?
Here is the code where the text is being posted
$postpost = $result["post_user"];
echo " <b>name</b> = " .$result["username"] ."<br>";
echo " <b>post</b> = " .$result["post"] . "<br>";
echo " <b>date</b> = ".$date["date"]. "
<br><br>----------------------<br><br>";
and here is the output problem
Use wordwrap() :
// the 80 is the number of characters after which to break:
echo " <b>post</b> = " . wordwrap($result["post"], 80, '<br>') . '<br>';
You may also be interested in nl2br().
$postpost = $result["post_user"];
echo " <b>name</b> = " .$result["username"] ."<br>";
echo " <b>post</b> = " .chunk_split($result["post"],100,"<br/>") . "<br>";
echo " <b>date</b> = ".$date["date"]. "
<br><br>----------------------<br><br>";
This will break it after 100 characters. You can change the 100 to your needs.
But wordwrap is a better solution :)
You can use wordwrap function in php
<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
?>
echo implode('<br/>',str_split($string,100));
Firstly, if you want the text to have a maximum width, enclose the whole thing in a wrapping <div> element, and use CSS to style it to a maximum width. Any text inside the element with then wrap nicely.
Here's the CSS code you'd need:
#mydiv {width:200px;}
The only text that will still be a problem after that is text without any spaces in it, which would still stretch off the edge of the page.
For these, you can use another CSS property, word-wrap, like so:
#mydiv {word-wrap:break-word;}
The best practice is to keep your CSS code separate from your HTML, but if you're not using stylesheets, you can add the CSS code directly to the <div> element with the style attribute, like so:
<div style='width:200px; word-wrap:break-word;'>
..... (your content goes here) .....
</div>
It is, of course, possible to do word-wrapping in PHP, using it's wordwrap() function (or str_split() for long strings with no spaces), but you'll end up with the lines being varying lengths when displayed on the page, because the font has different widths for different characters. Therefore, I would say that the CSS solution is better because the word wrapping will look better on the page.
Hope that helps.
this should do it :
$newtext = wordwrap($text, 20, "<br />\n");
examples can be found here
Use php's wordwrap() function.
http://php.net/manual/en/function.wordwrap.php
Edit: I initially thought this is a CSS problem. I see that you want to break strings which are very large with php. You can use wordwrap() for that, as artlung recommended.
What I initially thought in CSS: put each blog entry into a div, and assign word-wrap: break-word; for the div and also specify width of the div.
My idea on jsfiddle.com: Link

Categories