strtolower and similar functions with wordpress php - php

Hi am trying to figure out how to use the strolower function and strtolower + str_replace function with the following code - <?php echo get_the_author_meta('custom_field_35', $user->ID); ?>
This is what I have so far but it's not working -
<?php
$str = "echo get_the_author_meta('custom_field_35', $user->ID);";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so
?>
<?php $get_the_author_meta('custom_field_36', $user->ID) = strtolower(str_replace(",", "",$get_the_author_meta('custom_field_35', $user->ID))); ?>
How can I use strtolower and str_replace with get_the_author_meta?

Your strtolower and str_replace at the bottom look fine, but you can't assign to this:
$get_the_author_meta('custom_field_36', $user->ID)
You would be better off just having:
<?php echo strtolower(str_replace(",", "",$get_the_author_meta('custom_field_35', $user->ID))); ?>
Or putting it into a variable and echoing that:
<?php $var = strtolower(str_replace(",", "",$get_the_author_meta('custom_field_35', $user->ID)));
echo $var; ?>

Ok looks like I did not need to go back to learn basic PHP syntax rules, I was able to figure it out myself.
In case anyone else wants to know how I did it -
<?php
$f_states = get_the_author_meta( 'custom_field_35', $user->ID, true );
$f_pr = get_the_author_meta( 'custom_field_36', $user->ID, true );
?>
<?php Print(strtolower(str_replace(",", "",$f_pr))); ?> <?php Print(strtolower($f_states)); ?>

Related

How to echo php code and use it?

<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".

Run PHP and HTML code from a string

I have a string stored in a variable in my php code, something like this:
<?php
$string = "
<?php $var = 'John'; ?>
<p>My name is <?php echo $var; ?></p>
";
?>
Then, when using the $string variable somewhere else, I want the code inside to be run properly as it should. The PHP code should run properly, also the HTML code should run properly. So, when echoing the $string, I will get My name is John wrapped in a <p> tag.
You don't want to do this. If you still insist, you should check eval aka evil function. First thing to know, you must not pass opening <?php tag in string, second you need to use single quotes for php script You want to evaluate (variables in single quotes are not evaluated), so your script should look something like:
<?php
$string = '
<?php $var = "John"; ?>
<p>My name is <?php echo $var; ?></p>
';
// Replace first opening <?php in string
$string = preg_replace('/<\?php/', '', $string, 1);
eval($string);
However, this is considered very high security risk.
So if I'm right, you have the $var = 'John'; stored in a different place.
Like this:
<?php $var = 'John'; ?>
And then on a different place, you want to create a variable named $String ?
I assume that you mean this, so I would suggest using the following:
<?php
$String = "<p>My name is ".$var." </p>";
echo $String;
?>
You should define $var outside the $string variable, like this
<?php
$var="John";
$string = "<p>My name is $var</p>";
?>
Or you can use . to concatenate the string
<?php
$var="John";
$string = "<p>My name is ".$var."</p>";
?>
Both codes will return the same string, so now when doing
<?php
echo $string;
?>
You will get <p>My name is John</p>

Variable in php echo

I would really need help with a primitive thing.
I need this:
<?php echo do_shortcode('[cycloneslider id="<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
echo $my_meta['odkaz']; ?>"]'); ?>
I do not know how exactly do it.
I made it by this solution:
<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);?>
<?php echo do_shortcode('[cycloneslider id="'.$my_meta['odkaz'].' "]'); ?>
Is it right or..Could you edit it to right way?
I suppose you don't see clear your own code.
If that happens, then adopt a more verbose way of programming and create variables that describe more the values:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
$idCycloneSlider = $my_meta['odkaz'];
$doShortCodeResult = do_shortcode('[cycloneslider id="'.$idCycloneSlider.'"]');
echo $doShortCodeResult;
?>
That way we see clearly what happens in each line and the string concatenation is more simple to do.
Concatenate string and variables with the . operator:
$var = "Hello, I am made up of " . $some . $variables . " and strings";
You can write it using a string interpolation:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
echo do_shortcode("[cycloneslider id=\"{$my_meta['odkaz']}\"]");
?>
Or using a heredoc if extra spaces are ok:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
$shortcode = <<<TEXT
[cycloneslider id="{$my_meta['odkaz']}"]
TEXT;
echo do_shortcode($shortcode);
?>

Using substring to remove characters from a dynamic variable

I am trying to use substr to remove 4 characters from a URL string. The goal is to remove .jpg from the string and replace it with "-220x124.jpg".
I am using a wordpress plugin, advanced custom fields, but that is not the issue here. The issue is that the subst is not working with the Advanced custom fields code, the_sub_field. It returns the entire URL string without the last 4 characters removed. Any idea why?
Code below:
<?php if(get_field('still_uploads')): ?>
<?php $i = 0; ?>
<?php while(the_repeater_field('still_uploads') && $i <= 0 ): ?>
<?php
$imagejesse = the_sub_field('still_image');
$imagejessenew = substr($imagejesse,0,-4);
?>
<?php echo $imagejessenew.'-220x124.jpg'; ?>
<?php $i++ ?>
<?php endwhile; ?>
<?php endif; ?>
You can see an example here: http://gicreative-dev.com/blog/genre/gay/
Use strtr() function like that:
$imagejessenew = strtr($imagejesse, array(
'.jpg' => '-220x124.jpg',
));
See this for a proof: http://ideone.com/B8ZQe
Try this:
<?php
$imagejessenew = substr(trim(the_sub_field('still_image')),0,-4);
if($imagejessenew !== FALSE)
{
$imagejessenew .= '-220x124.jpg';
}
else
{
// Shit happened, the_sub_field('still_image') was shorter than 4 characters
}
?>
$imagejessenew = preg_replace('/(.*)(.(jpg|png|gif))$/i', '$1-220x124.$3', trim($imagejesse));

Getting echo within echo to work together

In a little trouble with this, is they any way i can get to echo's working together see code below,
<?php $youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE); ?>
<?php echo $video->embed(' <?php echo $youtube_vimeo_player['url']; ?> ', '', ''); ?>
I'm wanting the info brought from the vimeo_player url to be entered into the video->embed section.
Any help on this would much be appreciated : )
try it like this:
<?php $youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE); ?>
<?php echo $video->embed( $youtube_vimeo_player['url'], '', ''); ?>
Replace
' <?php echo $youtube_vimeo_player['url']; ?> '
with
"{$youtube_vimeo_player['url']}"
You dont need echoing inside the php string. Note that { and } are the special way to embed array index, or object method call into the string, they are not present in final string.
Btw, it's sufficient to just do
echo $video->embed($youtube_vimeo_player['url'], '', '');
As $youtube_vimeo_player['url'] already shoud be the string
<?php echo $video->embed("'".$youtube_vimeo_player['url']."'", '', ''); ?>
PHP isn't that crazy, but thanks for remembering us it could seem to.
Thou shall write:
<?php
$youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE);
$url=$youtube_vimeo_player['url'];
$video->embed($url, '', '');
?>
Educate yourself on the concept of variable, and remember, php has only one level of embedding (that is one level of <?php ... ?> — no nesting)
And believe it, it's better this way.

Categories