PHP Value Not Display if variable text includes apostrophe - php

I have the following piece of PHP code as part of my site theme. Where I am stuck is, if the value of $sec_title contains an apostrophe, then the text doesn't display. But without an apostrophe, it contains fine. How can I amend this to prevent this issue?
<?php
if ( $sec_title ) {
echo do_shortcode( "[onex_section_header
title={$sec_title}
subtitle='{$sec_subtitle}']"
);
} ?>

Try using:
$sec_title = addslashes($sec_title)
before you echo or try switching the double and single quotes around as so
echo do_shortcode('[onex_section_header
title={$sec_title}
subtitle="{$sec_subtitle}"]'
);

Related

handle single quote and double quote in jquery function

I have created one link from foreach loop in that I am showing one link with different parameters in jquery function
<?php
foreach ($questions as $row) {
if (!empty($row['url_ImageName'])) {
$url_ImageName = $row['url_ImageName'];
}else{
$Paragraph = $row['Paragraph'];
}
?>
Show Details
<?php
} ?>
function question_details(url_ImageName,Paragraph){
if (url_ImageName != '')
{
$(".exam-slideout .question-details img").attr("src",url_ImageName);
}
if (Paragraph != '')
{
$('.exam-slideout .question-details div').html(Paragraph);
}
}
in that first link which is created this:
Show Details
and the second link which is created this:
Show Details
in that, I have facing an issue with single quotes and double quotes.
to resolve this issue I have a try
$Paragraph = mysqli_real_escape_string($con, $row['Paragraph']);
But still function is not working with syntax error.
can anybody help me in this.
Just add a escape character (\) before the ' used in the middle of the string like:
Show Details
Alternative using Template Literals:
Show Details
You can learn more about Working with Strings in JavaScript.
You can use addslashes() on the $Paragraph variable, this will escape ' into \'. It will also escape ", so be a bit wary of it.
Show Details
Alternatively, replace all occurrances of ' to \' using str_replace().
Show Details
Live demo at https://3v4l.org/IFLnY

php string inside echo div tags

I'm trying to add a return policy custom field just above the add to cart button in woocommerce. I've got the following function:
<?php
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<div id="return-policy-wrapper">
<?php the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); ?>
</div>';
}
But the code validator points out there is an error somewhere in the string. I suspect the the error is with the single quote marks inside
<?php the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); ?>
I changed the single quotes in that string for double quotes. Now the string validation error is gone, but the function won't work.
Are they the single quotes that are causing the error and how can I fix it?
You're already in a <?php ... ?> context. Simply build your string. For example
printf('<div id="return-policy-wrapper">%s</div>',
get_cfc_field('rp-info-meta', 'rp-info-custom-filed'));
or
echo '<div id="return-policy-wrapper">',
get_cfc_field('rp-info-meta', 'rp-info-custom-filed'),
'</div>';
Note, I've used get_cfc_field instead so the string is returned and not echo-ed directly.
Another approach would be
echo '<div id="return-policy-wrapper">';
the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); // this echoes the value
echo '</div>'
The script is in the quoted string passed to the echo command, and is therefore not treated as a script, but echoed along with the rest of the string.
This might work as intended:
<?php
add_action('woocommerce_single_product_summary', 'return_policy', 20);
function return_policy() {
$info = get_cfc_field('rp-info-meta', 'rp-info-custom-field');
echo '<div id="return-policy-wrapper">';
echo $info;
echo '</div>';
}
?>
You can't use <?php inside another <?php [...] ?> block.
You code should be similar to that:
<?php
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<div id="return-policy-wrapper">' . the_cfc_field('rp-info-meta', 'rp-info-custom-filed') . '</div>';
}
If you see carefully, HTML content is inside single quotes and never contains other single quoted characters (only double ones). Then, I concat the HTML text with the the_cfc_field() function that returns a string and then concat back with more html.

php and echoing out string with html tags. string space truncate string and also not working as a replacement

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

Single quote within single quotes PHP

I have a HTML achor tag like below:
echo '<a href="javascript:tempBuy('.$res_get_price[0][0].','.$res_get_price[0][1].','.$res_get_price[0][2].','.$dt_str.')">'.$res_get_price[0][0];
And the corresponding javascript function tempBuy() is
function tempBuy(rate,veg_name,market_name,dt)
{
alert(dt);
}
But the problem is it does not alert at all ! May be I need to include the variable names within single quotes in tempBuy() function. I tried tempBuy(\'var1'\,\'var2\'...) but it shows error. How can I able to to that. Thanks .
Source for the part shows like this:
<td width="120px" class="">56.0
</td>
<script>
function tempBuy(rate,veg_name,market_name,dt)
{
alert(rate);
}
</script>
You didn't wrap your javascript arguments in quotes. You need to wrap each variable in single quotes, since you used double quotes for "href" attribute. Another thing is that you didn't close up "a" HTML tag.
echo ''.$res_get_price[0][0].'';
If there is anything in your variables that is not a valid javascript literal you have to make it a string like:
echo '<a href="javascript:tempBuy(\''.$res_get_price[0][0].'\' ...
If there are ' in your variables you have to replace them with \' as well.
As you can see form the rendered output, you need to quote the last 3 arguments which are non-numeric. The correct output should be: javascript:tempBuy(56.0,'Apple','Bangalore','2013-05-18')
The corrected PHP code is:
echo ''.$res_get_price[0][0].'';`
echo "<a href=\"javascript:tempBuy('".$res_get_price[0][0]."','".$res_get_price[0][1]."','".$res_get_price[0][2]."','".$dt_str."')\">".$res_get_price[0][0];

WordPress - Executing PHP in Theme Options Panel

I have created a theme options panel for a client however they would like the ability to enter PHP code in the textareas and have it executed on the front end.
However, when they enter the code, it does not display properly in the front end, please see the following two screenshots:
http://i.imgur.com/alOAD.png
http://i.imgur.com/pYhW0.png
It looks like the code is being stripped when displayed on the front end. Its displayed using this code:
<?php global $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}
?>
<?php echo $ag_footer_top; ?>
How can I get it to work properly? Is it possible?
If I enter the following into the theme options:
<?php wp_nav_menu( array( 'theme_location' => 'first','fallback_cb'=> ” ) ); ?>
It gets saved INTO the database as:
<?php wp_nav_menu( array( \'theme_location\' => \'first\',\'fallback_cb\'=> ” ) ); ?>
And its displayed on the front end as:
\'first\',\'fallback_cb\'=> ” ) ); ?>
First of all you must prevent WordPress from adding slashes to your content, using something like:
update_option('my_option',stripslashes($_POST['my_option']));
The other thing is that you want your code to be executed... well I do not know how to do this exactly, but a lot of Plugins provide this functionality, like
Linkable Title Html and Php Widget
You should download the Plugin and figure out how it works.
Just take the content of the field from the database now and try to parse it in some way.
Use stripslashes to unescape the string before evaluating/displaying the code.
You should replace ” with ''
you can use this functon of PHP
PHP: eval();
you can take the code in textarea save it to options as string and whenever you want to execute it, retrieve from options and pass it to eval.
Thanks
-Shak
I personally use Exec-PHP. It should be quite easy for your clients to use.

Categories