How to wrap output of echo in a span - php

Excuse the basic question but I'm new to PHP! How can I wrap the below text in span to change the font size of all the output fields. I managed the Name field but due to the periods I can't get the others working. Hopefully I can just wrap the entire echo in the same span.
echo "<span style=\"font-size: 10px;\">$row->Name</span>"." ".$row->Country." ".$row->Location." ".$row->Category." ".$row->Rating." ".$row->Review." ".$row->StartDate." ".$row->EndDate." ".$row->URL."<br>";

Try some thing like this:
echo '<span style="color:red;">'.$variable.'</span>';

Related

PHP echo resulting in jumbled up string

Basically I want to create a div with specific styling using PHP.
I have the following intended style saved up in a separate string for easy editing:
$bg = "background: url('./flags/" . $country[$id[0]]["iso"] . ".png')no-repeat center center fixed;";
And this is the echo that generates the div:
echo "<div class='flag' style='" . $bg . "'></div>";
When I run this code, the div does appear, but the style part is all jumbled up and weirdly formatted, like so:
<div class="flag" style="background:url(\" .="" flags="" hk.png\')no-repeat="" center="" fixed;'=""></div>
What is causing this problem?
Thanks in advance.
I guess I'll repost this here, since it worked out:
In the $bg variable, you're encasing the url in single quotes -- but in your echo statement, you're doing the same thing for the style attribute. So when your $bg variable renders, it's closing the single quotes. I could be wrong, but I would try switching out the quotes in your $bg variable like this:
$bg = 'background: url("./flags ... etc ...

How to add a line gap in php echo statement?

This might be a easy one, i don't know. I'm trying to add a line sapce in css output. Here is the code
echo '<style type="text/css">\n';
echo get_option('custom_css');
echo '</style>';
But when i do this output comes as
<style type="text/css">\n.button{
margin-bottom: 22px;
}</style>
EDITED
Since this is inside style tag it won't add empty or new line i think.I want to have a new line between style tags. So tags will be in separate line, no other style will be collapsed with the same line to the style tag. How to do it?
IT should Look like this
<style type="text/css">
.button{
margin-bottom: 22px;
}
</style>
Just put your string in double quotes. Single quotes aren't parsed for looking special chars in it.
echo "<style type=\"text/css\">\n";
echo get_option('custom_css');
echo '</style>';
For example.
Or like other guys said - concatenate it with PHP_EOL constant
Try PHP_EOL instead of \n
echo '<style type="text/css">'.PHP_EOL;
&nbsp is your friend. Add it whereever you want the space.
Try like
echo '<style type="text/css"><br/>';
echo get_option('custom_css');
echo '<br/></style>';
Since this is inside style tag it won't add empty space i think.I want
to have a space between style tags. How to do it?
Add inside where you want the space to occur.

How do I extract a specific substring from a given HTML tag, without knowing for certain its length?

I want to do something like:
<?php
$text = "<font style='color: #fff'>";
$replaceandshow = str_replace("<font style=\"?\">", "the font style is ?", $text);
echo $replaceandshow;
?>
For example the ? is color: #fff, but I want that PHP will trace it by itself, Is it possible + If it's possible , How can I do that?
P.S: Someone gave me a code but it's now working, it displays a White page for me.
<?php
$colorstring = "<font style='#fff'>";
$searchcolor = preg_replace('[a-fA-F0-9]{3,6}','[font style=$1]Test[/font]',$colorstring);
echo $searchcolor;
Thanks for helping.
You are getting white page because error reporting is turned off. The error in your code is missing delimiter in preg_replace. And additionally, to use back-referencing you should enclose the expression required to match in parentheses.
preg_replace('/([a-fA-F0-9]{3,6})/','the font style is $1',$colorstring);
shall give the correct output.
You might consider using a more constrictive expression because the current expression is very open to matching other strings like "FFFont". Another thing to note is that the expression may result in output like.
<font style='color: the color is #fff'>
Try:
/<font style='color: #([a-fA-F0-9]{3,6})'>/
Since you need to pull basically any attribute out of any HTML you can use php XML parsing to do this.
<?php
$doc=new DOMDocument();
$doc->loadHTML("<html><body>Test<br><font style='color: #fff;'>hellow</font><a href='www.somesite.com' title='some title'>some site</a></body></html>");
$xml=simplexml_import_dom($doc); // just to make xpath more simple
$fonts=$xml->xpath('//font');
foreach ($fonts as $font) {
echo 'font style = '.$font['style']."<br />";
}
$as=$xml->xpath('//a');
foreach ($as as $a) {
echo 'href = '.$a['href'] . ' title = ' . $a['title']."<br />";
}
?>
That will return:
font style = color: #fff;
href = www.somesite.com title = some title
You can use a different foreach loop for each HTML tag you need to extract and then output any of the attributes you want.
Answer based on How to extract img src, title and alt from html using php?
This will work with simple style attributes:
$text = "<font style='color: #fff'>";
preg_match("/<font style=['\"]([^'\"]+)['\"]>/", $text, $matches);
echo "The font style is ".$matches[1];
For anything more complicated (ex: if it includes quotes), you'll need to use a HTML parser, such as http://www.php.net/manual/en/class.domdocument.php

Echo statement makes new line every echo

I have a basic php echo statment as belows which echos a thumnail 70*70 but it echos a new line everytime. Is there anyway to stop this?
echo "<div class='imageIcon'><a href='index.php?menu=GaleriaYVideo&catAll=$var_catergoryAll&image=".$var_showImageAll."'><img src=".$var_showImageIcnAll." ></a></div>";
Basically i want to echo all my thumbs so there on the same line.
Any help well appreciated!
You're wrapping your output in a block-level div element. Remove that, or set display:inline or display:inline-block and they will all be inline. Or float the elements and give them a width.
You probably don't have width and height set to the class.
Set something like
.imageIcon {
width:70px;
height:70px;
float:left;
}
you should add .imageIcon {display:inline;} to your style
It's not the echo that's causing the new line, it's the div element

Highlighting Text: How to echo HTML DOM element with all tags

I want to highlight specified keywords in the body of an HTML document. At first I used preg_replace to put a < span > around the keywords, but of course that caused problems if the keyword was part of a tag, like the letter "i" (as in < li >). So instead, I'm using DOM::loadHTMLFile(path) to load the document, and then use the preg_replace inside the values of each child.
So far, so good. I can echo out the plain text of the document with the appropriate words highlighted and no interference from tags. But I need to echo the entire body of the text including the tags after the changes, and I don't know how. Here's what I have so far:
if (file_exists('mss/'.$link_title)) {
$descfile = DOMDocument::loadHTMLFile('mss/'.$link_title);
foreach ($descfile->childNodes as $e) {
$desc_output = $e->nodeValue;
$desc_output = preg_replace($to_highlight, "<span class=\"highlight\">$0</span>", $desc_output);
}
echo ???
}
What should I echo?
If you change your code to:
$e->nodeValue = preg_replace($to_highlight, "<span class=\"highlight\">$0</span>", $e->nodeValue);
You can probably use:
http://php.net/manual/de/domdocument.savehtml.php
to output your entire html document.

Categories