printf function - php

I want to create an output as follows using the printf function:
***********************************************************************************
Here is my code for this line:
printf("%'*c <br>\n",42);
where 42 is the specified ASCII code for *. this command only generates one * not 100 of *.
Any suggestions?

echo str_repeat('*', 100) . '<br>';
There is no need for printf, since you are not doing any formatting. You're just printing some literal stuff.

I think you need to escape the %,
printf("\%'*c <br>\n",42);

I think what you are looking for is str_repeat(). Code would be as follows:
<?php
echo str_repeat("*", 100);
?>

There are many ways to do this, but what I think you're trying to do is;
printf("%'*100s <br>\n", "");
which will print 100 asterisks followed by an HTML line break.

Like some of the others have mentioned, you can use str_repeat() to do this.
However, if you need to use printf(), you can use %s in the format, and pass str_repeat in as a variable. So it'd be something like this:
printf("%s <br>", str_repeat('*', 42));

Related

Strange behaviour from ltrim() with forward slash

I need to remove the beginning part of a URL:
$test = "price/edit.php";
echo ltrim($test,'price/');
shows dit.php
Here is a codepad if you want to fiddle: https://codepad.remoteinterview.io/DominantCalmingBerlinPrice
Any ideas what is going on? I want it to echo edit.php of course.
ltrim removes ALL characters found, consider the following:
$test = 'price/edit.php';
echo ltrim($test, 'dprice/'); // outputs t.php
For this particular scenario, you should probably be using str_replace.
The second argument to ltrim() is a character mask (a list of characters) that should be removed. e is a character that should be removed and so it is removed from edit.
There are many string manipulations that you could use, however since this is a filename/filepath the correct tool is a Filesystem Function, basename():
echo basename($test);
For more information on the filepath check into pathinfo().
Hi I have faced the same problem some time ago and found this solution use it if it suits your need
<?php
$test = "price/edit.php";
echo ltrim(ltrim($test,'price'),'/');
output
edit.php
but i must say you should use basename as all type problem
<?php
$test = "project/price/edit.php";
// echo ltrim(ltrim($test,'price'),'/');// this will give oject/price/edit.phpedit.php
echo basename($test); // and it will generate edit.php

PHP add string to first and last

How to add " string into the first and last in PHP,
something like this :
hello world
into like this :
"hello" "world"
is there before-string and after-string function in PHP?
In PHP there are two basic ways to get output: echo and print.
In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.
For more datailes use http://www.w3schools.com/php/php_echo_print.asp
Simply use it like as
echo '"Hello" "World"';
You can simply use preg_replace like as
echo preg_replace('/(\w+)/',"\"$1\"","hello world");
Demo
try this:
<?php
$str="hello world";//your string
$str=str_replace(" ",'" "',$str); //replace all spaces with " "
echo '"'.$str.'"';//while displaying add double quotes in begning and end.
?>
There is no such function.
However you can try using split function on space of the string, then use implode() function with your glue or you can simply try using regex.Latter would give you a 1 line call to do this.

Add commas to an extracted value

I have a database where I need to store the price of items. Now when I get the values from the database, I would like commas added for the thousand value e.g convert 19000 to 19,000.
<?php echo $row['pprice'];?>
How can I change the format?
You can try using this.
echo number_format($row['pprice']);
Not sure if its correct but wont hurt to try and see if it works.
Or create a new variable for the $row['pprice'] so like this.
$commaPrice = $row['pprice'];
echo number_format($commaPrice);
I suggest to use money_format:
$number = 1234.56;
setlocale(LC_MONETARY, 'it_IT.UTF8');
echo money_format('%i', $number) . "\n";
Note that you need to specify your current locale. If you want to use different locales than yours, you must build them install them on the operating system.
$newNumber = number_format($number);

Write two or more instructions in one line with PHP

I have a very simple question for you guys.
After deep researches, I've found nothing useful to get a good answer.
I just really like to know the way to write multiple instructions/commands in the same line, with PHP.
Just to be clear, this:
<?php
if (true) {
echo "First,";
echo " second";
echo " and third.";
}
?>
shoud become this:
<?php
if (true)
echo "First," & echo " second" & echo " and third";
?>
So, the script above can execute three operations in one line of code.
I tried to use the "&" sign to append more instructions in the same line and it seems it works...
Is this the correct way to do what I want to do? May this cause any problems?
Thanks!
PS: the "echo" instruction is just as example (I know that you can merge strings just using the dot (.) sign
PHP puts no significance on a line break at all. All you need to do is remove the line break, everything else can stay exactly the same:
<?php if (true) { echo "First,"; echo " second"; echo " and third."; } ?>
The statements are already terminated and separated by ;.
No. It's not correct. echo is not a function, and is not something you can & together like that. It does, however, support comma-separated "arguments", so something like
echo 'first', 'second', 'third';
is entirely possible and totally valid PHP code.
Even if the & version was possible, you'd actually be LOSING efficiency, because you're doing 3 echo calls, and then trying to combine their non-existence return values. e.g. you'd be turning 3 operations into 5.

PHP stripping strings

I would like to strip everything in a string before a - and leave what's after that. For example:
15.11-101
I want to strip 15.11 and leave 101. I've tried a few different things, but can't seem to get it working, if anyone could help me out, that'd be fantastic :)
Cheers
Leanne
You could use something like this:
$result = substr($original, strpos($original, '-') + 1);
assuming it appears only once, you can use the explode function.
$s='15.11-101';
$a=explode('-',$s,1);
echo $a[1];
This should be the fastest way.

Categories