So I'm making comment section for my page, and I'm trying to make them work properly.
The problem is that when I save comment to DB, for example like this:
My comment
{
ohh
}
and when displaying it like echo $comment, I get:
My comment { ohh }
So I fixed this with:
nl2br(preg_replace('/(\r)|(\n)/', '<br>', $comment->text));
Now I get:
My comment
{
ohh
}
But I need to display the first variant. When I vardie it gives me:
string(36) "My comment
{
ohh
}"
In db it's storing comment with all indents.
So question: How do I correctly display comment with whitespaces. And also extra question, how I store these comments to DB to allow only 4 sequent whitespaces??, it doesn't matter on the beginning or in the middle.
One solution (possibly not ideal) would be to wrap the text in <pre></pre>, e.g.:
<pre><?php echo $comment; ?></pre>
Another solution (also possibly not ideal) would be to replace spaces with an HTML non-breaking space. You can wrap it around your "newlines to <br>" function:
str_replace(' ', ' ', preg_replace('/(\r)|(\n)/', '<br>', $comment->text))
You can convert whitespaces to before inserting into the database.
$_POST['code_field'] = str_ireplace(' ' , ' ' , $_POST['code_field']);
If you are allowing for editing of comments then convert back into regular spaces.
$row['code_field'] = str_ireplace(' ' , ' ' , $row['code_field']);
Related
There seems to be a bug in a Wordpress PHP function that leaves whitespace in front of the title of the page generated by <?php echo wp_title(''); ?> I've been through the Wordpress docs and forums on that function without any luck.
I'm using it this way <body id="<?php echo wp_title(''); ?>"> in order to generate an HTML body tag with the id of the page title.
So what I need to do is strip that white space, so that the body tag looks like this <body id="mypage"> instead of this <body id=" mypage">
The extra white space kills the CSS I'm trying to use to highlight menu items of the active page. When I manually add a correct body tag without the white space, my CSS works.
So how would I strip the white space? Thanks, Mark
Part Two of the Epic
John, A hex dump was a good idea; it shows the white space as two "20" spaces. But all solutions that strip leading spaces and white space didn't.
And, <?php ob_start(); $title = wp_title(''); ob_end_clean(); echo $title; ?>
gives me < body id ="">
and <?php ob_start(); $title = wp_title(''); echo $title; ?>
gives me < body id =" mypage">
Puzzle. The root of the problem is that wp_title has optional page title leading characters - that look like chevrons - that are supposed to be dropped when the option is false, and they are, but white space gets dumped in.
Is there a nuclear option?
Yup, tried them both before; they still return two leading spaces... arrgg
Strip all whitespace from the left end of the title:
<?php echo ltrim(wp_title('')); ?>
Strip all whitespace from either end:
<?php echo trim(wp_title('')); ?>
Strip all spaces from the left end of the title:
<?php echo ltrim(wp_title(''), ' '); ?>
Remove the first space, even if it's not the first character:
<?php echo str_replace(' ', '', wp_title(''), 1); ?>
Strip only a single space (not newline, not tab) at the beginning:
<?php echo preg_replace('/^ /', '', wp_title('')); ?>
Strip the first character, whatever it is:
<?php echo substr(wp_title(''), 1); ?>
Update
From the Wordpress documentation on wp_title, it appears that wp_title displays the title itself unless you pass false for the second parameter, in which case it returns it. So try:
<?php echo trim(wp_title('', false)); ?>
ltrim()
ltrim($str)
Just to throw in some variety here: trim
<body id="<?=trim(wp_title('', false));?>">
Thanks for this info! I was in the same boat in that I needed to generate page ids for CSS purposes based on the page title and the above solution worked beautifully.
I ended up having an additional hurdle in that some pages have titles with embedded spaces, so I ended up coding this:
<?php echo str_replace(' ','-',trim(wp_title('',false))); ?>
add this to your functions.php
add_filter('wp_title', create_function('$a, $b','return str_replace(" $b ","",$a);'), 10, 2);
should work like a charm
I select a list of names from mysqli database then display row details in display.php with if (isset($_GET['name']));
The link is
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?name=$str'>$str</a></td></tr>";
This executes correctly unless name contains '(apostrophe).
For instance $str (as input/click) shows as L'ECLIPSE but the <a> link only L'
The result in display.php is 'No data found for your request'
I have found exact same queries on this site but none of the answers have resolved my problem. Perhaps I am not implementing correctly.
I assume this is about escaping. But I know little about it.
<?php
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?name=".urlencode($str)."'>$str</a></td></tr>";
urlencode() the string first. So you don't get this kind of problems.
Try this code.
<?php
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?
name=".htmlspecialchars($str)."'>$str</a></td></tr>";
?>
Your Single quote becomes ' ;
I hope it will help
MySql: I have my products table set up as follow:
pg_id | pg_name
1 | Pizza's calzone
2 | Kids menu
Php: Echo out the html while looping through the records in the MySQL table.
<?php do { ?>
<li>
<?php echo "<a href=". "products.php?p_group=" .$row_getproductnames[ 'pg_name'] . ">"; ?>
<?php echo $row_getproductnames[ 'pg_name']; ?>
</a>
</li>
<?php } while ($row_getproductnames=mysql_fetch_assoc($getproductnames)); ?>
My hyperlink: The link to the products.php page should look like this for records with white space in it. This post and reference the product names correctly in the products page.
http://127.0.0.1/products.php?p_group=Pizza's calzone
But it truncates after the white space to
http://127.0.0.1/products.php?p_group=Pizza's
I have checked numerous samples like using in the place of the white space, Html encryption or decryption etc. Still having problem with getting the string to link correctly. Any help would be appreciated.
You need to quote the href with double quotes:
echo "<a href=\"products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "\">"
If you use single quotes or no quotes then the ' in pg_name is misunderstood by the browser.
Your not using quotes? I don't know for sure this is causing it but usually with any parsing issues quotes will fix it.
Try replacing this line:
<?php echo "<a href='products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "'>"; ?>
If you are trying to create a valid URL, you can will want to replace the spaces with a + or %20. Either will do. I also suggest removing the apostrophes:
$new_url = str_replace(" ","+", $old_url); //Replace space
$new_url = str_replace("'","", $new_url ); //Remove apostrophe
Edit:
If you are needing to use the name parameter to retrieve an item from the database, you can do it by 're-replacing' the space and apostrophe characters at the other end like this:
Build the url:
$new_url = str_replace(" ","+", $old_url); //Replace space
$new_url = str_replace("'","APOSTROPHE", $new_url ); //Remove apostrophe
Then at the page where you will perform the SELECT query:
$product_name = str_replace("+"," ", $product_name); //Put spaces back
$product_name = str_replace("APOSTROPHE","'", $product_name ); //put apostrophes back
There are however much easier ways to send values to other pages such as sending a POST request
I'm trying to limit the title into 1 word only
I tried using this code but it doesn't return anything..
<?php
$title = get_the_title();
$names = explode(' ', $title);
echo $names[0];
?>
This is where I'm trying to implement the code If I change the get_the_title() into $names[0] but it doesn't return anything.
$postheader .= '<h3 class="entry-title">' . get_the_title() . '</h3>';
What is in the content of $title? The explode/split should work in that fashion, so there are a couple of things you can do to try to debug:
First, check the content of $title. You can do this as simply as outputting is and viewing source: make sure it has actual spaces, and not non-breaking spacing, or that the field does not start with a space.
Second, make sure your first code block is actually getting executed by echo'ing something you can check for in source, or any other method to ensure it is running. Good luck!
I started doing a new project in PHP / MySql . The Aim of this project is to manage articles for a magazine that i am the editor of.
So the content of the articles, i decided i would store with the "TEXT" column type of MySql
Now when i retrieve this column and print it with echo, the newlines are not there. Its all on the same line.
$resset = mysql_query("select txt from articles where id = 1");
$row = mysql_fetch_assoc($resset);
$txt = $row['txt'];
echo $txt; //doesnt print it as it is in the database as it was typed (multiline)
Find below, the text as it looks in the database and as it looks when it is echoed
in the databse, it is with new lines http://img413.imageshack.us/img413/4195/localhostlocalhostzxcvb.jpg
Text as it looks when echod http://img718.imageshack.us/img718/1700/sdashboardmozillafirefo.jpg
But within the database, its stored with newlines.
Has anybody else encountered this problem?
Please help me as my project depends on this :|
Whitespace in HTML is folded into a single space. Use nl2br() if you want to maintain newlines in HTML.
Have you tried
echo $txt."<br/>";
alternatively, you can put your output between <pre> tags:
echo "<pre>";
$resset = mysql_query("select txt from articles where id = 1");
$row = mysql_fetch_assoc($resset);
$txt = $row['txt'];
echo $txt; //doesnt print it as it is in the database as it was typed (multiline)
echo "</pre>";
btw,
echo $txt."<br/>";
may not work since it will just append newline at the end but not within $txt string
I know this isn't part of your question, but if you additionally want new lines in your HTML code but not your presentation, use double quotes with \n. This can help keep the HTML really tidy.
echo "\n";