PHP text truncate limit characters - php

I have $Description field is a long text from MySql database. Following displays the full text but I really want to display only 50 characters instead of full text length.
<?php echo $row['Description'];?>
I have tried several different options but I don't get any results.
I hope someone can guide me through this.
Thank you for your time and help.
Sincerely,

Use substr function:
<?php echo substr($row['Description'], 0, 50);?>

You can use a substr
<?php echo substr($row['Description'], 0, 50); ?>

Related

How do I truncate this result?

this displays the name of a group, but sometimes the names of the groups are really long and break the layout. How can I limit the result to maybe 20 characters? Thank you!
<?php echo $group->getName();?>
There is a php function for it. It's substr(). Your example code would look like:
<?php $length = 20; ?>
<?php echo substr($group->getName(), 0, $length);?>
If you want, you can add some extra features like check the length and if it's longer than $length than cut it and add "..." for users to know, the text was cut.
I found this to work. Sorry this question seems to have annoyed some of you, but I want others to know how I fixed it.
<?php echo substr($group->getName(),0,25);?>

How to truncate end of SKU in Magento on frontend?

What I'm seeking to do is display the SKU of a product on the frontend of my Magento cart, which is working accurately with this code:
<?php echo $this->htmlEscape($_product->getSku()) ?>
However, what I need to do in addition to this is truncate the last character returned when the SKU is loaded on the front end. How can I achieve this with PHP, what code can I use to accomplish this, and how would I insert it into this string to truncate the last character from the output?
Thanks for any help in advance! :]
To truncate the last character in a string call substr with -1 for the second parameter.
<?php echo $this->htmlEscape(substr($_product->getSku(), 0, -1)) ?>

PHP ARGB to Alpha-Hex and Alpha-Hex to ARGB

I got the following color -16777216 and I want to convert it to a alpha-hex code like this 00FFBBCC in PHP. I also want to be able to do the reverse. I really don't know where to start and my friend Google has no answer for me.
Any one can help please ?
Thank you.
What about using dechex()?
echo dechex(-16777216);
It outputs ff000000
If you want uppercase letters, simply use strtoupper():
echo strtoupper(dechex(-16777216)); //FF000000
Edit: to do the reverse, use hexdec() instead of dechex()

How to use regex in PHP to get int after slash in string

I've tried to create a code in PHP that gets the topic id after the forward slash in a given string. However the problem i'm having is that its returning nothing, how can i make it return the int?
echo preg_match('/([^/]+)/', 'Learning-English/478', $discussion_id);
echo $discussion_id;
This is for an online forum, thank you for your help; it's much appreciated. If you need ay more information please don't hesitate to leave a comment.
preg_match("#/(\d+)$#", 'Learning-English/478', $discussion_id);
That would work for you.
To print the id(matched number); you need to echo $discussion_id[1].
Here is a working link.
For the newer strings, you wouldn't be needing the string end match($). Thus, the regex will be:
preg_match("#/(\d+)#", 'Looking-for-Pen-Pals/1161&t=viewDiscussion', $discussion_id);
echo $discussion_id[1];
you could just use explode() like this:
$arr = explode('/',$string);
$discussion_id = $arr[count($arr)-1];
this will split your string and then get you the last part.

url gets cropped before reaching maximum lenght with serialize/php arrays

I am learning php. I have some code where I am trying to post 2 variables and 2 arrays from one php page to another page, the recieving page works fine but the first page is cropping data after a few characters (it hasnt reached max lenght or anywhere close)-
Here, $array_name and $array_qty are two dynamic arrays. I have verified that echo $c gives me exactly what I want.
<?php
$serialized_name=serialize($array_name);
$serialized_qty=serialize($array_qty) ;
$c="count=".$count ."&&Sum=" . $a . "&&serialized_name=". $serialized_name . "&&serialized_qty=". $serialized_qty;
echo $c;
?>
echo $c gives me-
count=6&&Sum=45.91&&serialized_name=a:7:{i:0;s:7:"vanilla";i:1;s:7:"vanilla";i:2;s:21:"very berry strawberry";i:3;s:7:"vanilla";i:4;s:7:"vanilla";i:5;s:7:"vanilla";i:6;s:7:"vanilla";}&&serialized_qty=a:7:{i:0;s:1:"2";i:1;s:1:"1";i:2;s:1:"1";i:3;s:1:"1";i:4;s:1:"1";i:5;s:1:"3";i:6;s:1:"3";}
However, this gives me cropped output of $c=
<p><a href="Checkout.php?<?php echo $c ?>" >Checkout</a> </p>
The cropped output that i get from the above line is-
http://localhost/myRestaurant/Checkout.php?count=6&&Sum=45.91&&serialized_name=a:7:{i:0;s:7:
I think I should have gotten this-
http://localhost/myRestaurant/Checkout.php?count=6&&Sum=45.91&&serialized_name=a:7:{i:0;s:7:%22vanilla%22;i:1;s:7:%22vanilla%22;i:2;s:21:%22very%20berry%20strawberry%22;i:3;s:7:%22vanilla%22;i:4;s:7:%22vanilla%22;i:5;s:7:%22vanilla%22;i:6;s:7:%22vanilla%22;}&&serialized_qty=a:7:{i:0;s:1:%222%22;i:1;s:1:%221%22;i:2;s:1:%221%22;i:3;s:1:%221%22;i:4;s:1:%221%22;i:5;s:1:%223%22;i:6;s:1:%223%22;}
I know get is not the best most secure way but I think this should have worked. Any tips on what I am doing wrong and how to fix it will be appreciated.
Your quotation mark will end the attribute prematurely. Escape your quotation marks:
<?php echo htmlentities($c); ?>
You should use POST for that kind of shopping cart product, but anyway, if you want to just create a URL out of your arrays, you can't just serialize arbitrary data, you'll need to encode the data or it will break because of special chars.
Just use for encoding:
$serialized_name = base64_encode(serialize($array_name));
$serialized_qty = base64_encode(serialize($array_qty));
For retrieveing the data, in Checkout.php, do the opposite:
$unserialized_name = unserialize(base64_decode($_GET["serialized_name"]));
$unserialized_qty = unserialize(base64_decode($_GET["serialized_qty"]));

Categories