Is there a way to line break a certain sentences on the result text on pdo to break them in to two or more paragraphs each results..trying to achieve something like this pic
While my output was this one
I tried the answers in similar topic like this one Multiple paragraphs
using the code similar to
$content = $row['content'];
$breakpoint = round($content.length / 2); // half of the string length
$first = substr($content, 0, $breakpoint);
$second = sbustr($content, $breakpoint);
But it gives me a "undefined constant length" error Y.Y
my code block to load two results column names "details" and "more_details"
<?php if(isset($_GET['page'])):?>
<?php foreach($courses as $row):?>
<h1><?php echo $row['Fullname'];?></h1>
<?php echo $slug;?>
<hr><p>
<?php echo $row['details'];?>
</p>
<br>
<p>
<?php echo $row['more_details'];?>
</p>
<?php endforeach;?>
found an answer thanks to forbs but i'm willing for another method if there would be another way
answer
$content = $row['content'];
$breakpoint = round(strlen($content)/ 2); // half of the string length
$first = substr($content, 0, $breakpoint);
$second = substr($content, $breakpoint);
Well here's the answer for undefined constant length.
That's because content.length is Javascript, and you are in php.
$breakpoint = round(strlen($content)/ 2);
That should fix it, though that's gotta look very strange to be broken in the middle of a word.
Related
I'm trying to replace the first 4 digits of a number with X, for example:
$num= 1234567890
I want the output to appear like this: XXXX567890
I have tried the function:
$new = substr($num, 0, -4) . 'xxx';
but It only removes the last 4 digits so what should I do ?
You can use the same in opposite
$num= 1234567890;
$new = 'xxxx' . substr($num, 4);
echo $new;
second parameter tells about starting point for string and parity(positive or negative) tells about direction. positive number means to right of string and negative number means to left of string.
http://php.net/manual/en/function.substr.php
With substr_replace function:
$num = 1234567890;
print_r(substr_replace($num, 'XXXX', 0, 4)); // XXXX567890
Another solution is to use str_pad which "fills up" the string to 10 elements with "X".
$num= 1234567890;
Echo str_pad(substr($num,4), 10, "X",STR_PAD_LEFT);
https://3v4l.org/tKtB7
Or if the string lenght is not always 10 use:
Echo str_pad(substr($num,4), strlen($num), "X",STR_PAD_LEFT);
I think this one can be helpful for achieving desired output.
Solution 1:
Try this code snippet here
<?php
ini_set('display_errors', 1);
$num= 1234567890;
echo "XXXX".substr($num, 4);//concatenating 4 X and with the substring
Solution 2: Try this code snippet here
<?php
ini_set('display_errors', 1);
$num= 1234567890;
$totalDigits=4;
echo str_repeat("X", $totalDigits).substr($num, $totalDigits);// here we are using str_repeat to repeat a substring no. of times
Output: XXXX567890
If have written a tiny function to do tasks like this.
function hide_details($str, $num = 4, $replace = 'x') {
return str_repeat($replace, $num).substr($str, $num);
}
echo hide_details('1234567890');
I never intended to have to use PHP - heck, two days ago I didn't even know what it was, so excuse me if any of my terminology is weird or incorrect.
My predicament is that I need a working code that will end up outputting a list of writer names (that's why the array is called "writers") that are linked to their writer pages/profiles. Here's what I have so far that correctly displays the list of writer names:
<?php
$writers = get_post_meta($post->ID, 'writer', false);
$last = array_slice($writers, -1);
$first = join(', ', array_slice($writers, 0, -1));
$both = array_filter(array_merge(array($writers), $last), 'strlen');
echo join(' and ', $both);
?>
And the correct writer page link can be generated with this code:
<?php echo get_post_meta($post->ID, 'writerlink', true); ?>
I just don't have the skill to get these puzzle pieces to fit together. Since there are often multiple writers, there will often be more than one writer link. Is it possible to have each list/array value to have its correct link attached?
This question already has answers here:
a method of selecting random characters from given string
(15 answers)
Closed 6 years ago.
I just start to learn PHP, but I can't to make a easy problem. Can you help me and after say why my problem doesn't work? Here is the problem:
Create a new variable $name and store your name in it.
Then print a random character from your name. Use your knowledge of strlen(string), rand(min, max), and substr(string, start, length) to do this.
HINT: Remember that substr() treats characters in a string as a zero-indexed array (first letter is at position zero). This means that the last character in the string will be at position length - 1.
I try but without result.
<html>
<p>
<?php
$name = "George";
$fl = substr($name, 0, strlen($name));
$sl = substr($name, 0, 1);
print rand($fl,$sl);
?>
</p>
</html>
I think problem is at rand...I can't randomise items with id or something?I can print the $sl or $fl ..
It can be done like this:
<?php
$name = "George";
$nameLength = strlen($name);
$randomNumber = rand(0, $nameLength - 1);
$randomLetter = substr($name, $randomNumber, 1);
echo $randomLetter;
?>
Source: https://stackoverflow.com/a/23915981/5798798
if less than x don't show a "..."
<?php echo substr(stripslashes($row['news_title']), 0, 20). ".."; ?>
I have it to show more than x if more than 20, but it shows "..." when there's 10 chars. Is there anyway I could have it not to show?
any tutorials?
Thanks!
Try like
<?php echo substr(stripslashes($row['news_title']), 0, 20);
if(strlen($row['news_title']) > 20)
echo "..";
?>
You could use CSS tricks, but this would be the code for doing it server-side:
if (strlen($row['news_title']) <= 20) {
echo htmlspecialchars($row['news_title']);
} else {
echo htmlspecialchars(substr($row['news_title'], 0, 20)), '...';
}
Note that strlen() counts bytes and not characters per se; this is important when you start working with Unicode, in which case you may want to consider using mb_strlen().
Btw, using stripslashes() is somewhat of a red flag; if your quotes come out as escaped, the problem lies somewhere else and shouldn't be a problem of the presentation layer ... in fact, you should be using htmlspecialchars() instead.
This would do.
<?php echo strlen(stripslashes($row['news_title']))>20 ?substr(stripslashes($row['news_title']), 0, 20)."...":stripslashes($row['news_title']); ?>
Try to write your own sub string function:
it can be somthing similar like this
http://www.sranko.com/nwP3LFit
In my page I have some post previews from RSS feeds. Every post preview shows about 300 characters. When a user clicks on expanding button, then the #post-preview is replaced with the #post. The #post shows the rest of the post.
Everything fine with this but the format of the #post is not good, not readable. So I thought of allowing <br><b><p> tags, it will make it ok to be read. Because I don't want the user to be distracted, I want the tags to be allowed after the 300 chars.
With the following method, it is possible to break some tags where the $start ends and $rest starts. This means no good readable output.
$start = strip_tags(substr($entry->description, 0, 300));
$rest = strip_tags(substr($entry->description, 300), '<b><p><br>');
$start . $rest;
My question is how can I keep $start and $rest the same (no tags) until the 300 char, and after that $rest will show the formatted post? Are there any other ways of doing this?
Here is an example of a RSS feed structure (from view page source).
<item><guid isPermaLink="false"></guid><pubDate></pubDate><atom:updated></atom:updated><category domain=""></category><title></title><description></description><link></link><author></author></item>
I am looking for a way that does not kill performance.
Something like:
$start = substr($entry->description, 0, 300);
if(($pos = stripos($start, "<")) !== false) {
$start = strip_tags(substr($start, 0, $pos));
$rest = substr($entry->description, $pos);
}
else {
$start = strip_tags($start);
$rest = substr($entry->description, 300);
}
Ok, it's just a concept. Gets first 300 chars and checks for broken tag. If broken cut before it and get $rest from this point. If not broken just strip and get rest. There is at least 1 problem:
you never now the length of the $start(after strip_tags could be nothing left), could use loop with length checking but eeee... efficiency
EDIT
Ok, get it:
$start = "";
$chars = 400;
while(strlen($start) < 300) {
$start = strip_tags(substr($rss, 0, $chars));
$chars += 50;
}
$pos = stripos($rss, substr($start, strlen($start) - 50));
$rest = substr($rss, $pos+50);
Ok, little nasty and there are some cases on which it fails(with repetable text probably:D), tested on Ideone