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
Related
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);?>
My example code is given below.
$a= "I think there are many people who can help in my difficulty. breakword Thank you in advance. brwakword If i got this solution I will be happy. Breakword I have already searched in google about it I did not get it's solution. Breakword If you have any link of solution please help me. breakword Thank you again!";
From the above string I want to get the sentence between 2nd and 3rd 'breakword' word.
It means I want to extract 'If i got this solution I will be happy.'.
If you have any solution please help me. Thank you in advance
You can split your string into smaller parts using explode() and get the "Nth" part of the array.
$sentences = explode('breakword', $a);
echo $sentences[2];
PHP > 5.4 will support this too :
echo explode('breakword', $a)[2];
a similar approach but intead of using explode you can also use preg_split
supports (PHP 4, PHP 5)
preg_split('/breakword/', $a);
for the second answer you can use
$dummy=array();
preg_match_all('/breakword/', $a,$dummy)
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()
simply put i want to know why the first echo is giving back \x02 and the second echo doesn't give back something when it is exactly the same. why is the first echo not recognized as hex?
$test = "02";
echo "\x".$test;
echo "\x02";
hope someone can solve this. has been bothering me for a while
The answer is what was posted in my other post about this subject.
Link to question:create nameserver hex header
credits go to glglgl :)
sorry for the trouble
I'm current making a website with a photo album in it. The website in 2 languages, english and dutch. So I made language file like:
$lang['hello'] = 'Hallo'; //Hallo is hello in dutch
With the photo album I'm trying use the same principle like:
$lang['discription_001'] = 'photo of a house';
With showing the images I made a counter, now I want to use the same counter in the dispription like so:
echo $lang['discription_'$counter]
And $counter being 001 for photo number one. However this does not work, Could someone tell how I could get this to work, or any other method to get what I want.
Thanks in advance, Thomas de Zeeuw
P.S. I'm new in PHP, however I normally pick up things quite fast, so make some explaintion would be appreciated.
You're almost there:
echo $lang['discription_' . $counter]
The . is PHP's concatenation operator, for combining strings.
You just forgot the string concatenation operator . to concatenate 'discription_' and the value of $counter:
$lang['discription_'.$counter]
You have a typo.
echo $lang['discription_'$counter]
Should be
echo $lang['discription_' . $counter];
You could get it to work by simple repairing your code:
echo $lang['discription_'.$counter];
or
echo $lang["discription_{$counter}"];
Is $counter a string?? If so your example should work fine if you fix the parse error (You missed the concatenation operator (.).
It would be much better if you showed us actual code from your application.
echo $lang['discription_' . $counter];
As you have got answers already you forgot the concatenation operator, but better way in my mind would be to use multidimensional array.
echo $lang['descriptions'][$counter];