How do I truncate this result? - php

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);?>

Related

PHP's addcslashes ignoring some chars

I would like to know why the function addcslashes() is ignoring certain characters.
As you will notice in the output at the bottom, ["`","$","""] are being ignored.
This is my example:
<?php
$ADPasswdRaw = $_GET["element_3"]; #data from a web form
$ADPasswd = addcslashes($ADPasswdRaw, "~`!##$%^&*()_+=-][}{\\|:;\"',./<>?");
echo $ADPasswd;
?>
Output
\~\`\!\#\\\#$\%\^\&\*\(\)\_\+\-\=\;\:"\'\<\>\?\,\.\/
Thanks
This must be a problem with my input.
This is unclear and old at this point.

PHP text truncate limit characters

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); ?>

string name out of vlaue of another string php

in PHP I have the following case:
<?php $anz1='3';$i='1';echo $anz.$i;?>
I don't know how to echo $anz1 with the help of $i. I must do it this way, this is just an easy case, I need it for loops where it echos every $anz
Thanks in advance
I hope I could write it understable for you, my english is not the very best.
Maybe this is what you're looking for:
$anz1='3';
$i='1';
echo ${'anz'.$i};
Output:
3

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.

How to read this php code?

How to decode this string in php?
$x = "h\164tp\163\072\057\057\x70r\157\x64\x75\x63t-\x73\x65a\x72\143\150\056\x61\160i\x2e\x63\x6a\x2ec\x6f\x6d\057v\062/\160\x72\157\x64\165\x63t\x2ds\x65\141\162\143\x68\x3f";
It looks like a regex URL, but how to read what it is?
Thanks.
just echo it out.
<?
$x = "h\164tp\163\072\057\057\x70r\157\x64\x75\x63t-\x73\x65a\x72\143\150\056\x61\160i\x2e\x63\x6a\x2ec\x6f\x6d\057v\062/\160\x72\157\x64\165\x63t\x2ds\x65\141\162\143\x68\x3f";
echo $x;
It outputs:
https://product-search.api.cj.com/v2/product-search?
Print it!
% cat test.php
#!/usr/bin/env php
<?php
$x = "h\164tp\163\072\057\057\x70r\157\x64\x75\x63t-\x73\x65a\x72\143\150\056\x61\160i\x2e\x63\x6a\x2ec\x6f\x6d\057v\062/\160\x72\157\x64\165\x63t\x2ds\x65\141\162\143\x68\x3f";
print $x;
?>
% ./test.php
https://product-search.api.cj.com/v2/product-search?
It is actually a string with some characters specified in hexadecimal and octal notation. Just echo it.
just print it, its a url
https://product-search.api.cj.com/v2/product-search?
Well, I created a PHP page as follows:
<?php
$x = "h\164tp\163\072\057\057\x70r\157\x64\x75\x63t-\x73\x65a\x72\143\150\056\x61\160i\x2e\x63\x6a\x2ec\x6f\x6d\057v\062/\160\x72\157\x64\165\x63t\x2ds\x65\141\162\143\x68\x3f";
print $x;
?>
and ran it.
And I got the following:
https://product-search.api.cj.com/v2/product-search?
Which means nothing to me, except that cj.com is part of Commission Junction, which is an online advertising network.
It's been deliberately obfuscated, so clearly the person who wrote it intended that you didn't notice it or understand it, and would leave it alone. I don't know the context of the question, why you're asking about it, but my guess would be that you've been hacked and someone has inserted this code (and more) into your site.
If that's the case, their aim would clearly be to gain some advertising revenue by freeloading on your site. Not particularly malicious as hacks go, but not something you'd want to be happening (especially if you don't know what kind of ads would be shown).
Simple:
utf8_decode();
http://www.php.net/manual/en/function.utf8-decode.php

Categories