I have a sentence in PHP that I've broken down into its word components using explode.
foreach ($words as $splitword)
{
echo $splitword;
/* echo "<a href = 'word.php?word=" . $splitword . "'>" . $splitword . "</a>"; */
echo " ";
}
What I want to do is make each word clickable, so I echo each word followed by a space. Now, using the code you see above, everything looks fine and natural. However, if I uncomment the commented line, and comment out echo $splitword;, so that I'm now echoing links instead of just the word, things get ugly:
echo " " no longer works, I have to use echo " "
The spaces are very large. Echoing each word without making it a link produces natural spacing, the way it should be. But when I start using the a tag followed by  , the spaces are about twice as wide.
Any idea why, and what a workaround is?
echo " " does not work because you are writing HTML, which does not parse " ".
You'll have to use like you already discovered.
The extra spacing could be your styling. Check your CSS. If you still don't find anything strange, add this to your CSS:
a {
margin: 0;
padding: 0;
}
Related
I have the following code snippet:
echo "<label><input type='checkbox' class='selector' name='{$data['ColA']}'>" .preg_replace('/[^0-9]+/','', $data['ColA'])."</label>";
Here I'd like to use preg_replace in "name" tag as well. How can I make it work? I tried the same code in name tag but it doesn't work. Thanks.
The output of this code is like this:
<label>
<input type='checkbox' class='selector' name='7b'>7</label>
I need to remove "b" from 7 in name tag too.
Using functions inside of this syntax with {} is not really a good idea and may lead to some issues you can avoid in the most simple way, just terminate the string, concatenate it with your function output and the rest of the string, like so:
echo "<label><input type='checkbox' class='selector' name='" . preg_replace('/[^0-9]+/','', $data['ColA']) . "'>" . preg_replace('/[^0-9]+/','', $data['ColA'])."</label>";
So in general:
echo "Something: " . a_function($variable) . ", the rest of the string.";
Edit: and one thing I'd forget about, depending on what your data is, you may want to use htmlspecialchars function on in before inserting it anywhere into your HTML DOM, if it's user-provided data, in order to avoid XSS attack.
I echo out an image like that:
$newString = $thumbPre.'profilemain'.$thumbPost;
echo "<img src='http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error'>";
Now i want the image as a background-image, i tried it like that, but it doesn´t work:
echo '<div style="background-image:url('http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error');"></div>';
you need to use backslashes for nested apostrophes
echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\' alt=\'error\');></div>';
Firstly, remove alt='error' because background-image does not have an alt parameter, img does (you probably thought you could use that from your original code). In trying to use that, your background will not show up.
And your background won't show unless you have content inside that div. I've added Content as an example.
echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\');">Content</div>';
You either have to escape the encapsulating quotes, or remove them altogether.
echo '<div style="background-image:url(http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.');">Content</div>';
Error reporting would have also thrown you a parse error such as:
Parse error: syntax error, unexpected '' alt='' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
http://php.net/manual/en/function.error-reporting.php
This is going to look super confusing but you need to 1, escape the quotes and 2, concatenate your path within that,
let me give you an example. I misread a bit of the question but this will serve you well moving forward especially for cleanliness sake
here is an example:
$imagePath ='PATH TO IMAGE HERE';
echo '<div style="background-image:url(\'' .$imagePath. '\')" >STUFF HERE </div>';
Im new to learning PHP as you might have guessed. I have the contents of a .txt file echoed but I would like it to stand out more, so I figured I would make it a different colour.
My code without colour:
<?php
$file = fopen("instructions.txt", "r") or exit("Unable to open file");
while(!feof($file))
{
echo fgets($file);
}
fclose($file);
?>
I have researched this and seen suggestions to others to use a div style, however this didn't work for me, it gave me red errors all the way down the page instead! I think its because I'm using 'fgets' not just a variable? Is there a way to colour the echo red?
The code I tried but doesn't work:
echo "<div style=\"color: red;\">fgets($file)</div>";
(In general) You need to separate the actual PHP code from the literal portions of your strings. One way is to use the string concatenation operator .. E.g.
echo "<div style=\"color: red;\">" . fgets($file) . "</div>";
String Operators
Other answer already told that you can't use a function call in a double quoted string. Let additionally mention that for formatting only tasks a <span> element is better suited than a <div> element.
Like this: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span
You should try:
<div style="color: red;"><?= fgets($file);?></div>
Note: <?= is an short hand method for <?php echo fgets($file);?>
This version does not need to escape double quotes:
echo '<div style="color:red;">' . fgets($file) . '</div>';
You can do this with the concatenate operator . as has already been mentioned but IMO it's cleaner to use sprintf like this:
echo sprintf("<div style='color: red;'>%s</div>", fgets($file));
This method comes into it's own if you have two sets of text that you want to insert a string in different places eg:
echo sprintf("<div style='color: red;'>%s</div><div style='color: blue;'>%s</div>", fgets($file), fgets($file2));
I am trying to style a sentence within php by trying to echo the div. However the styling is not being applied. The css is correct, I think I have something wrong on how I am calling a div withing php. Can you please advise.
This what I have tried:
echo '<div id="sumtitle">' . "<tr><td>" . "This is a test" . "</td></tr>" . '</div>';
tried also like this:
echo '<div id="test">Test sentence</div>';
thanks
PHP is not responsible for styling.
You have to check your styles file or whatever.
Check your HTML and CSS.
PHP has nothing to do here.
The code looks OK, although the multiple use of the . in the first is unnecessary. You could just put:
echo '<div id="sumtitle"><tr><td>This is a test</td></tr></div>';
BTW: Are you using a <table> for your <tr> and <td>?
Regardless, the styling should work fine if you use something like:
#sumtitle { font-style: italic; }
or
#test {...}
I'd double check the CSS! :)
I am echoing back these 2 variables in to a table, but wanted to know how to add a line break between these 2?
echo $row ['username'] . $row ['date_time'];
if you're printing an HTML output to your browser:
echo $row["username"]."<br />".$row["date_time"];
EDIT:
when printing to HTML - you better print the variables after passing them through htmlspecialchars function in order to avoid Cross-site-scripting (XSS), I'll do it this way:
echo htmlspecialchars($row["username"],ENT_QUOTES)."<br />".htmlspecialchars($row["date_time"],ENT_QUOTES);
if you want to print it to a file or something similar:
echo $row["username"]."\n".$row["date_time"];
you can always echo normal html from php.
echo "<div id=\"mydiv\"> Div content goes here </div>";
note the backslashes for double quotes wrapping mydiv to escape them.
so you can add a tag in between them to get a new line.
echo $row["username"] . "<br />" . $row["date_time"];