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;
  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.
Related
Need some suggestions for the methodology to achieve the following:
My current script gets text and if it has URL(s) then it replaces them. The issue is I want to truncate the URLS(s) so they do not break the width of a table or unsightly line break to fit them.
$text = file_get_contents("temp.txt");
$link = preg_replace('#(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)#', '$1', $text);
echo $link;
I am concerned that if I substr() the $link then it won't work if multiple URLs are found. Can you PHP the $1 in the replacement? Any alternatives?
Use preg_replace_callback to modify the match and replacement. This returns the first 10 characters as an example:
$link = preg_replace_callback('#(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)#',
function($m) {
return ''.substr($m[1], 0, 10).'';
},
$text);
This kind of problem can also be solved on client side using css (I assume you are speaking about the html element table in your question).
To do that, you have to give your cell a fixed size and to set the display property to inline-block. Then you can define the behaviour of the cell when a word is too long using the white-space, overflow and text-overflow properties.
Example:
<html>
<head>
<style>
.mytable td:nth-child(2) {
display: inline-block;
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<table class="mytable">
<tr><td>abcd</td><td>www.smallurl.jp</td><td>efgh</td></tr>
<tr><td>ijkl</td><td>www.a-giant.url/larger/than/the/cell/width</td><td>mnop</td></tr>
</table>
</body>
</html>
The HTML looks like this:
$html = 'SOME TEXT<p style="border-top: 0.0px;border-right: 0.0px;vertical-align: baseline;border-bottom: 0.0px;color: #000000;padding-bottom: 0.0px;padding-top: 0.0px;padding-left: 0.0px;margin: 0.0px;border-left: 0.0px;padding-right: 0.0px;background-color: #ffffff;">SOME TEXT';
I tried strip_tags($html, '<p>'); to remove everything except for <p> but that preserves all the style elements of the tag.
I want the above to be replaced with just <p>
What's the best approach?
Thanks!
The simplest solution for this would be something based on preg_replace().
$html = 'SOME TEXT<p style="border-top: 0.0px;border-right: 0.0px;vertical-align: baseline;border-bottom: 0.0px;color: #000000;padding-bottom: 0.0px;padding-top: 0.0px;padding-left: 0.0px;margin: 0.0px;border-left: 0.0px;padding-right: 0.0px;background-color: #ffffff;">SOME TEXT';
$html = strip_tags($html, '<p>');
$html = preg_replace('/\sstyle=["\'][A-Za-z0-9-:\s.;#]{1,}["\']/', '', $html);
As always, you should always be somewhat careful when trying to parse html with regex. For instance, this would fail if for some reason the text inside the <p /> tag contained something formatted like a css style. (Something like <p>If I typed style="color:red" inside the tags, it would also be removed</p>)
The next step to make something like this better would be to actually parse the string as an XML document using the DOMDocument class. It depends on how robust a feature set you are looking to achieve. However, this method could change your string in unexpected ways; for instance, parsing your string as a DOMDocument would cause a </p> tag to be added. That kind of validation may or may not be useful for you.
also some time very useful will remove style tag like below
`$html = '<style>li {
list-style: none;
background: url(/images/Articles/ordasoft_discount.png) no-repeat;
padding-left: 30px;
}
li p{font-size:16px}
ul li {
padding-left: 30px;}
.portfolio-container{width: 45%;}
</style> Hello word ';`
this will help:
`$html = preg_replace('/<style>[A-Za-z0-9-:\s.\/_;#\(\)\{\}%]{1,}<\/style>/', '',$html) ;`
I have a description field in one of my table in my database. I want to display it in a paragraph form, however, the problem is that if the string is too long, I want to break it and continue the string in a new line. How am I going to do it?
<p align="justify" style="font-size:15px; text-decoration: underline">
<?php echo $item[0]->description;?>
</p>
Try wordwrap
<?php echo wordwrap($item[0]->description,30,"<br>\n");?>
Here new line will be there automatically on every 30th character ,you can change it to your convinience
You can try this;
.text
{
background-color:red;
width:100px;
word-wrap: break-word;
}
here's the jsfiddle;
New Line
word-wrap to break the line.
p{word-wrap: break-word;}
Hope this will work for you.
You can try wordwrap in php http://www.w3schools.com
/php/func_string_wordwrap.asp
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");
?>
When I used a module i have some problems. Some white space appeared
$content='';
$vocab = vuagame_getVocByName("the_loai_game");
$terms = taxonomy_get_tree($vocab->vid);
foreach ($terms as $term) {
$content.="<div class='contentBox'>";
$content.="<div class='contentBoxHeader'>";
$content.="<a href='/category/".$term->tid."'>".$term->name."</a>";
$content.="</div>";
$content.="<table class='gameTable' cellspacing='0' cellpadding='5'>";
$content.= views_embed_view('home', 'page', $term->tid);
$content.="</table>";
$content.="<div class='seemorelink'><a href='/category/".$term->tid."'>Xem thêm</a></div>";
$content.="</div><br/>";
}
return $content;
and here's the result with inspect element :
<div class="contentBox"><div class="contentBoxHeader">18+</div>
"
"
<table class="gameTable" cellspacing="0" cellpadding="5"><tbody><tr>
Does anyone can find out my trouble? Thanks a lot!
first, br tag is not a standard now.
debug it like this,
1)try the html without the loop first. or break the loop after its first cycle.
if white space still exits,
replace a href tag with
"<a href='lorum.html'>ispum </a>"
and see if its gone.. there might be a chance of irregular tag open/close
if still it exists,
replace all data objects with just dummy text, if might have some faulty data.
What do you mean by "some white space appeared"? You get something displayed, but there is white space underneath it?
If that's the case, you could try with adding "clearfix" class to your output and adding this in your css file:
.clearfix:after {
content: "";
clear: both;
font-size: 0;
display: block;
height: 0;
visibility: hidden;
}
And yes, this seems unecessary, that could cause the problem too.
It's seems to be a encoding issue. Try to make sure you're saving your .tpl files with encode UTF-8
This should fix weird spaces.
Im new to PHP and I can't figure out what the rules are for using the echo function. For example, if I need to echo a large block of css/js, do I need to add echo to each line of text or is there a way to echo a large block of code with a single echo?
When I try to echo a big block of code like this one, I get an error:
if (is_single()) {
echo '<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/><style type="text/css">
.form-label{
width:150px !important;
}
.form-label-left{
width:150px !important;
}
.form-line{
padding:10px;
}
.form-label-right{
width:150px !important;
}
body, html{
margin:0;
padding:0;
background:false;
}
.form-all{
margin:0px auto;
padding-top:20px;
width:650px !important;
color:Black;
font-family:Verdana;
font-size:12px;
}
</style>
<link href="http://jotform.com/css/calendarview.css" rel="stylesheet" type="text/css" />
<script src="http://jotform.com/js/prototype.js" type="text/javascript"></script>
<script src="http://jotform.com/js/protoplus.js" type="text/javascript"></script>
<script src="http://jotform.com/js/protoplus-ui.js" type="text/javascript"></script>
<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script>
<script src="http://jotform.com/js/location.js" type="text/javascript"></script>
<script src="http://jotform.com/js/calendarview.js" type="text/javascript"></script>
<script type="text/javascript">
JotForm.init(function(){
$('input_6').hint('ex: myname#example.com');
});
</script>';
}else {
}
Is there a better way to echo large blocks of code without a lot of work (adding echo to each line for example)?
Heredoc syntax can be very useful:
// start the string with 3 <'s and then a word
// it doesn't have to be any particular string or length
// but it's common to make it in all caps.
echo <<< EOT
in here is your string
it has the same variable substitution rules
as a double quoted string.
when you end it, put the indicator word at the
start of the line (no spaces before it)
and put a semicolon after it
EOT;
One option is to get out of the php block and just write HTML.
With your code, after the opening curly brace of your if statement, end the PHP:
if (is_single()) { ?>
Then remove the echo ' and the ';
After all your html and css, before the closing }, write:
<? } else {
If the text you want to write to the page is dynamic, it gets a little trickier, but for now this should work fine.
Check out heredoc. Example:
echo <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo <<<"FOOBAR"
Hello World!
FOOBAR;
The is also nowdoc but no parsing is done inside the block.
echo <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
Echoing text that contains line breaks is fine, and there's no limit on the amount of text or lines you can echo at once (save for available memory).
The error in your code is caused by the unescaped single quotes which appear in the string.
See this line:
$('input_6').hint('ex: myname#example.com');
You'd need to escape those single quotes in a PHP string whether it's a single line or not.
There is another good way to echo large strings, though, and that's to close the PHP block and open it again later:
if (is_single()) {
?>
<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/><style type="text/css">
.form-label{
width:150px !important;
}
.form-label-left{
width:150px !important;
}
.form-line{
padding:10px;
}
.form-label-right{
width:150px !important;
}
body, html{
margin:0;
padding:0;
background:false;
}
.form-all{
margin:0px auto;
padding-top:20px;
width:650px !important;
color:Black;
font-family:Verdana;
font-size:12px;
}
</style>
<link href="http://jotform.com/css/calendarview.css" rel="stylesheet" type="text/css" />
<script src="http://jotform.com/js/prototype.js" type="text/javascript"></script>
<script src="http://jotform.com/js/protoplus.js" type="text/javascript"></script>
<script src="http://jotform.com/js/protoplus-ui.js" type="text/javascript"></script>
<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script>
<script src="http://jotform.com/js/location.js" type="text/javascript"></script>
<script src="http://jotform.com/js/calendarview.js" type="text/javascript"></script>
<script type="text/javascript">
JotForm.init(function(){
$('input_6').hint('ex: myname#example.com');
});
</script>
<?php
}else {
}
Or another alternative, which is probably better for readability, is to put all that static HTML into another page and include() it.
Man, PHP is not perl!
PHP can just escape from HTML :)
http://www.php.net/manual/en/language.basic-syntax.phpmode.php
if (is_single()) {
//now we just close PHP tag
?>
</style>
<script>
<blah blah blah>
<?php
//open it back. here is your PHP again. easy!
}
?>
I wonder why such many people stuck to ugly heredoc.
Your problem is actually caused by:
$('input_6').hint('ex: myname#example.com');
You need to escape the single quotes to be \'
However: Using a Heredoc is a much better idea, as it will be much cleaner overall.
To expand on #hookedonwinter's answer, here's an alternate (cleaner, in my opinion) syntax:
<?php if (is_single()): ?>
<p>This will be shown if "is_single()" is true.</p>
<?php else: ?>
<p>This will be shown otherwise.</p>
<?php endif; ?>
Just break out where you need to.
<html>
(html code)
<?php
(php code)
?>
(html code)
</html>
Do not use shortened-form. <? conflicts with XML and is disabled by default on most servers.
I prefer to concatenate multiple Strings together. This works either for echo AND for variables.
Also some IDEs auto-initialize new lines if you hit enter.
This Syntax also generate small output because there are much less whitespaces in the strings.
echo ''
.'one {'
.' color: red;'
.'}'
;
$foo = ''
.'<h1>' . $bar . '</h1>' // insert value of bar
.$bar // insert value of bar again
."<p>$bar</p>" // and again
."<p>You can also use Double-Quoted \t Strings for single lines. \n To use Escape Sequences.</p>"
// also you can insert comments in middle, which aren't in the string.
.'<p>Or to insert Escape Sequences in middle '."\n".' of a string</p>'
;
Normally i start with an empty string and then append bit by bit to it:
$foo = '';
$foo .= 'function sayHello()'
.' alert( "Hello" );'
."}\n";
$foo .= 'function sum( a , b )'
.'{'
.' return a + b ;'
."}\n";
(Please stop Posts like "uh. You answer to an five jears old Question." Why not? There are much people searching for an answer. And what's wrong to use five year old ideas? If they don't find "their" solution they would open a new Question. Then the first five answers are only "use the search function before you ask!" So. I give you another solution to solve problems like this.)
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);
You can achieve that by printing your string like:
<?php $string ='here is your string.'; print_r($string); ?>