Hey I have on my database a text with 800 chracters when I echo it out it display in just one line getting too big and bad for read,
echo mysql_result(mysql_query("SELECT `message` FROM `pm` WHERE `pm_id` = '1'"));
How do I put or \n automatic on the end of every line because there is differents datas inserted in the database and do manualy for every one is hard.
edit:
I want to meke it like: one \n after 100 characters or something like that.
Use the wordwrap function: http://www.php.net/manual/en/function.wordwrap.php
echo wordwrap($data, 100, "\n"); // Or use "<br/>" in html
There also is a 4. parameter. If you data does not contain any spaces, wordwrap can not break between words. If you set the 4. parameter to true, wordwrap will cut no matter if within a word or not.
// cut after 100, no matter if inside a word.
echo wordwrap($data, 100, "\n", true);
put it in a div, and set div width. That will solve your problem. Like this:
#CSS
.sample{
width:50px;
}
and here's some sample html
<div class="sample">
#your results from database here
</div>
Related
I've currently got a few DB entries which look like this:
1. This is some text http://www.sitehere.com more text
2. Text https://www.anothersite.com text text text
3. http://sitehere.com http://sitehereagain.com
4. Just text here blabla
I am trying to filter those entries while printing them and add infront of all the urls http://anothersite.com/?. Also put the new url destination as link but keep the original url as text:
text text http://sitehere.com text
Until now I've managed to add the http://anothersite.com/? part with the following code:
$result = preg_replace('/\bhttp:\/\/\b/i', 'http://anothersite.com/?http://', $input);
$result = preg_replace('/\bhttps:\/\/\b/i', 'http://anothersite.com/?https://', $input);
But the ahref is not the way I want it. Instead it is:
text text http://anothersite.com/?http://sitehere.com text
PS: I am not looking for a javascript solution :) Thank you!
This following code should work. There are a few large changes I made. The first one is I am using preg_replace_callback instead of preg_replace so I am able to properly encode the URL and have more control over the output. The other change is I'm matching the whole domain so the callback function can insert the URL between the <a> tags and also can add it to the hyperlink.
<?php
$strings = array(
'This is some text http://www.sitehere.com more text',
'Text https://www.anothersite.com text text text',
'http://sitehere.com http://sitehereagain.com',
'Just text here blabla'
);
foreach($strings as $string) {
echo preg_replace_callback("/\b(http(s)?:\/\/[^\s]+)\b/i","updateURL",$string);
echo "\n\n";
}
function updateURL($matches) {
$url = "http://anothersite.com/?url=";
return ''.$matches[1].'';
}
?>
Hello I am trying to change CSS content on some DIVs depending of their class name.
To explain better, I have a while loop in PHP reading from the database to output DIVs and I have a field named "section" with data such as A,B,C,D,E,F,G.
For the DIVs located in section "A" and "B" I want the class name to be desk_box_hor (for horizontal) ELSE I want it to desk_box_ver(vertical).
Below is what I tried doing only two sections (A,B) that need to be vertical. The others need to be horizontal. If theres a more efficient way of doing this please tell me. I have about 200 of these DIVs being output on screen.
If you have a better title please recommend one, I didn't know what to put lol.
Thanks in advance!
My fiddle of what I want both DIVs to look like
PHP:
while($row = mysqli_fetch_assoc($desk_coord_result)){
//naming X,Y values
$id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
$sec_name = $row['section_name'];
//draw a DIV box at its X,Y coordinate
//if section A and B do vertical
if($sec_name == 'B' || $sec_name == 'A'){
echo '<div class="desk_box_ver" data="'.$id.'" style="position:absolute;left:'.$x_pos.'px;top:'.$y_pos.'px;">id:'.$id.'</div>';
}
//else do it horizontal
else{
echo '<div class="desk_box_hor" data="'.$id.'" style="position:absolute;left:'.$x_pos.'px;top:'.$y_pos.'px;">id:'.$id.'</div>';
}
} //end while loop for desk_coord_result
CSS:
/* desk boxes*/
.desk_box_ver{
width: 23px;
height: 10px;
border: 4px solid black;
padding:10px;
}
.desk_box_hor{
width: 10px;
height: 23px;
border: 4px solid black;
padding:10px;
}
Also, lets say I want to use these two classes in the same Jquery function, is this the proper way of doing it?
$(".desk_box").add(".desk_box_ver").click(function() {
or
$(".desk_box, .desk_box_ver").click(function() {
In answer to your question "If theres a more efficient way of doing this please tell me." (I'll leave your other questions to someone else) yes, there are more efficient ways to write this PHP code, which then makes debugging and maintenance easier:-
a) Instead of two very long echo strings which are almost exactly the same, introduce a new PHP variable, say, $class. Then write:
$class = 'desk_box_hor';
if($sec_name == 'A' || $sec_name == 'B'){
$class = 'desk_box_ver';
}
echo '<div class="' . $class . '" data="' . $id . '" style="position:absolute;left:' . $x_pos . 'px;top:' . $y_pos.'px;">id:' . $id . '</div>';
Now you only have one long echo string to write, and to debug.
Also my preference is (though this is opinion only) to put a space either side of all those string concatenation dot operators - it makes it easier to decipher whats going on.
b) The next improvement you can make is to swap all the single and double quotes. Then you can write a single string with no concatenation operators at all, as you can put a PHP variable inside double quotes. Again, it makes the string of html clearer and easier to read, and debug. (Browsers can handle single or double quotes in the HTML tags). You end up with:
echo "<div class='$class' data='$id' style='position:absolute;left: $x_pos" . "px;top: $y_pos" . "px;'>id:$id</div>";
c) Next we can make the HTML code being created more readable; at the moment your script is generating a huge block of HTML markup (200 divs?) with no line breaks. Horrendous to debug. Just add \n to the end of your echo string like so:
echo ".....id:$id</div>\n";
and that will split the generated HTML markup into separate lines (but the onscreen text will be unaffected). Much easier then to see if one of the items went wrong (for instance, if the database returns an empty value for one of the records, it will stand out in the HTML like a sore thumb). Note that you can add \n inside a string surrounded by double quotes; if you stay with the original single quoted string you would have to add with a dot operator:
.....id:$id</div>' . "\n";
d) Lastly, you could cut out all those 200 position:absolute strings from the generated HTML markup by putting it into your CSS stylesheet, just retaining the CSS values that vary. Ie:
.desk_box_hor, .desk_box_ver { position: absolute; }
As regards why you are only getting vertical divs, never horizontal, I'll just try an educated guess. Are you really getting back from the database what you think you are?
For instance, you say in your comments the field name is "section", but the PHP is looking for a "section_name" field. Or is the data itself wrong? Have you got PHP error checking on, eg error(reporting(E_ALL)? If not, it would not return an error message, but still blindly go on reading through all rows in the db.
In that case it will always take the else part of your if...else. Supposedly this is the horizontal div path, but because the CSS has the width and height values the wrong way round (see above) it will actually produce vertical boxes all the time.
What is the best method for taking a text string (from a mysql database, like a wordpress post or page) and dividing the text into sections of 500 characters (or any other number of characters), and then outputting each 500-character section wrapped in its own div container?
Being new to PHP, I'm not sure how to go about segmenting the content based on character count. We could get the character count using:
<?php
$content = get_the_content(); //wordpress function for post content as string.
echo strlen($content);
?>
But that is as far as my knowledge goes. Can anyone help me out with this challenge?
Let's say I was requesting a page of content from the wordpress database, and it was 5,000 characters long. The goal would be to output 10 divs or spans that each contained 500 characters.
THANK YOU in advance for any assistance.
PHP offers a function for that, it is called 'str_split()'. You can use it like this:
<?php
$content = get_the_content();
$chunks = str_split($content, 500);
//Printing each chunk in a div
foreach($chunks as $chunk_content) {
echo "<div>";
echo $chunk_content;
echo "</div>";
}
?>
More info to str_split: http://www.php.net/manual/en/function.str-split.php
EDIT:
If words should not get cut in the middle, use this function instead:
<?php
$content = get_the_content();
$strings = wordwrap($content, 500, "{BREAK}"); //Put a {BREAK} every 500 characters
$chunks = explode("{BREAK}", $strings); //Put each segment separated by {BREAK} into an array field
//Printing each chunk in a div
foreach($chunks as $chunk_content) {
echo "<div>";
echo $chunk_content;
echo "</div>";
}
?>
If you want to save some memory, you can combine these functions like this:
<?php
foreach(explode("{BREAK}", wordwrap(get_the_content(), 500, "{BREAK}")) as $chunk) {
echo "<div>" . $chunk . "</div>\n"; //The \n creates a new line
}
?>
For more information concerning wordwrap see http://www.php.net/manual/en/function.wordwrap.php
I have a problem with counting words in PHP, this is my code:
$word=substr(stripslashes(strip_tags($row['short_story'], '<a></a>')), 0,100 )."...";
$tpl->set( '{word}',$word);
on that code i just can show URL links in my result, i need to show complete style and HTML codes, so i changed to this:
$word = substr( stripslashes (strip_tags($row['short_story'], '<a><b><i><u><br></a><div></div>')), 0,400 )."...";
i changed this:
<a></a>
to:
<a><b><i><u><br></a><div></div>
But problem is, i have to set word limit over 400-500 if i want to show my result correct! because with 100 0r 200 word limit, HTML codes will be broken!
My question is, how i can use word count, but counting only numbers and letters? for example in this code:
<div style="color:red;">hello</div>
i need to count only hello word. is that possible?
Try the below code: it will work only when you know what tags are included in your string.
<?php
$tag_word = '<div style="color:red;">hello</div>';
// You can add any number of tags ex: strip_tags($tag_word, '<div><a><img><p>');
$word = strip_tags($tag_word, '<div>');
echo count($word);
?>
I have a textarea with input like this:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
I want an output with line breaks like this:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
How can I do that?
In my previous question, someone suggested wordwrap, which would be useful if the line had spaces.
Yes, use wordwrap...
$text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$newtext = wordwrap($text, 10, "<br />", true);
echo $newtext;
I don't know if I understand the question correctly, but HTML textarea field also has the wrap attribute (values "soft", "hard", or "off"). Hard wraps the words inside the text box and places line breaks at the end of each line so that when the form is submitted it appears exactly as it does in the text box.
So:
<textarea cols="30" rows="5" wrap="hard">text..</textarea>
you could insert a "<br /> at the index, where the break should occur - only for the output.
textbox.value = HandleLineBreaks(line, index);