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.
Related
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
This question already has answers here:
Simple PHP stuff : variable evaluation [duplicate]
(5 answers)
Closed 11 months ago.
Basically, I want to add a PHP variable in HTML, with that HTML already being inserted into a constant, which is in PHP code. Here's what I mean: (obviously this code below is wrong, but imagine I would want to be inserting the $VARIABLE in the URL of the iFrame, for example)
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/$VARIABLE'></iframe>";
?>
What would be the syntax for adding that variable in there?
Thanks in advance!
This is just a basic template. str_replace() should do the trick.
const EXAMPLE = "<iframe src='http://example.com/{{{VARIABLE}}}'></iframe>";
$variable = 'example-sub-category
$merged_content = str_replace('{{{variable}}}', $variable, EXAMPLE);
Note I used {{{}}} to denote the insert. This is not PHP syntax, but you will find templates often use something like that the would not be expected in the text otherwise to denote placeholders.
Have you tried:
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/".$VARIABLE."'></iframe>";
?>
You can insert PHP anywhere in HTML just by defining its tags <?php ?>
<iframe src='http://example.com/<?php echo $VARIABLE; ?>'></iframe>
Using your example:
const EXAMPLE = "<iframe src='http://example.com/" . $VARIABLE . "'></iframe>";
For this kind of task I'd better use sprintf:
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/%s'></iframe>";
echo sprintf(self::EXAMPLE, $VARIABLE);
To move in and out of php within your html, use <? [php code here...] and ?> to end php and continue with html. You can escape characters with a back slash \ and using single quotes as Rujikin mentioned above. Or, within PHP, you can echo your html so that you're not bouncing in and out of php.
For example:
some php...
echo '<span style=\"color:#980000\"><strong>\"{$searchTerm}\"</strong></span>';
more php...
Notice that the html is enclosed in single quotes (end-to-end) and inside the single quotes, double quotes are used where required by html, e.g., for the <style> tag.
This example is basically saying, "echo whatever the value of php variable $searchTerm is, put it in quotes that's required by echo, and make it dark blue (#0000FF) and bold (<strong>).
I hope this helps. :)
You can try something like:
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/$VARIABLE'></iframe>";
$newHtml = str_replace('$VARIABLE', $VARIABLE);
?>
As you mentioned your code was fake, so I guess what you need is to replace some places in your HTML strings by a content which is unknown until you php script is executed, right ?
so what you need is to put some place holders and change them later, as I showed in my snippet above.
Hope this works
I have a problem with the "windows.location" command in JavaScript. I would like to add the php variable in the link windows.location. How can i do?
For example: I would like to transfer user to English page or Vietnamese Page by variable $lang
Here is my code
echo 'window.location="/B2C/$lang/confirm_fone.html"';
and the result in address bar is:
http://10.160.64.4:1234/B2C/$lang/confirm_fone.html
the $lang in address bar cannot be decode?
Variables in single-quoted strings don't get interpolated in PHP.
Use this instead:
echo 'window.location="/B2C/' . $lang . '/confirm_fone.html"';
Or use doublequotes:
echo "window.location='/B2C/$lang/confirm_fone.html'";
This is because the whole string is in single quotes.
You'll want to use double quotes for interpolation.
Otherwise, you can try:
echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"';
If you put php variables within the string you should use Double Quotes .
echo "window.location='/B2C/$lang/confirm_fone.html'";
You have to concatenate the value, as follows :
echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"';
Variables are not resolved by PHP in Strings when you use the ' as delimiter. Use " instead (and ' for the javascript command) or concatenate the String using ..
If you are in php-context:
echo "window.location=\"/B2C/"{$lang}"/confirm_fone.html\";';
If you are in HTML-Context (or better "outer-php-context"):
window.location="/B2C/<?php echo $lang ?>/confirm_fone.html";
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));
Is there a better way to output data to html page with PHP?
If I like to make a div with some var in php, I will write something like that
print ('<div>'.$var.'</div>');
or
echo "'<div>'.$var.'</div>'";
What is the proper way to do that?
Or a better way, fill a $tempvar and print it once? like that:
$tempvar = '<div>'.$var.'</div>'
print ($tempvar);
In fact, in real life, the var will be fill with much more!
There are 2 differences between echo and print in PHP:
print returns a value. It always returns 1.
echo can take a comma delimited list of arguments to output.
Always returning 1 doesn't seem particularly useful. And a comma delimited list of arguments can be simulated with multiple calls or string concatenation. So the choice between echo and print pretty much comes down to style. Most PHP code that I've seen uses echo.
printf() is a direct analog of c's printf(). If you're comfortable in the c idiom, you might use printf(). A lot of people in the younger generation though, find printf()'s special character syntax to be less readable than the equivalent echo code.
There are probably differences in performance between echo, print and printf, but I wouldn't get too hung up on them since in a database driven web application (PHP's typical domain), printing strings to the client is almost certainly not your bottleneck. The bottom line is that any of the 3 will get the job done and one is not better than another. It's just a matter of style.
you can even write
$var = "hello";
echo "Some Text $var some other text";
// output:
// Some Text hello some other text
or
print("Some Text $var some other text");
// output:
// Some Text hello some other text
doesn't make big difference. This works with double-quotes only. With single quotes it doesn't. example:
$var = "hello";
echo 'Some Text $var some other text'; // Note the single quotes!
// output:
// Some Text $var some other text
or
print('Some Text $var some other text'); // Note the single quotes!
// output:
// Some Text $var some other text
Just try this you gonna love the well formated amount of infos :
<?php
echo '<pre>';
var_dump($your_var);
echo '</pre>';
?>
OK, I explain : set a "code" html format and var_dump show the value, the type, the params ... of the variable.
http://us2.php.net/echo
<div><? print($var); ?></div>
Or if you don't have short tags on, you might need to
<div><?php print($var); ?></div>
if you have the short_open_tag option enabled you can do
<?=$var?>
But some find that messy.
You can also use the following syntax:
echo <<<ENDOFTEXT
<div>
$var
</div>
ENDOFTEXT;
Just make sure the ENDOFTEXT is not indented.
You could do something like this:
<div><?php echo $var; ?></div>
One of the nice things about PHP is that you can insert it into regular HTML, and accomplish things like the above with ease. I've always used echo myself in PHP. Not sure if it's the "proper" way, but it's the easiest.
While echo and print are almost equal, you a using different values. Your first value will result in
<div><value of $var><div>
while the second will result in
'<div>'.<value of $var>.'<div>'
But the rest is semantically almost equal. Since echo and print are no real functions but special language constructs, the parenthesis in your first example is just wrapping the single string value and not the parameter list.
See also https://stackoverflow.com/questions/1462581#1462636 and https://stackoverflow.com/questions/1163473#1163793.
I have read somewhere that echo is faster that print. But its just too small of a performance gain.