Using substring to remove characters from a dynamic variable - php

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));

Related

Truncate up to 8 characters, change to small words, remove special characters

email: emDSDla.ccount#gmail.com
<?php
$useruser = $customer->email;
?>
<?php echo $useruser ?>
I need to create from customer email, I need to create username.
With rule:
get only first 8 characters
remove special characters # .
change to small words
result:
emdsdlac
Can anyone help me implement this to code?
update:
<?php $useruserBIG = 'emDSDla.ccount#gmail.com' ?>
<?php $resultsmall = strtolower($useruserBIG); ?>
<?php echo $resultsmall ?>
<?php
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', $resultsmall);
$newstr = str_replace("#", '', $resultsmall);
?>
<?php echo $newstr ?>
Currently I get result:
emdsdla.ccountgmail.com
Can anybody help me improve this code (merge in one and correct with truncate) to get
result:
emdsdlac
Thanks!
update3:
currently I get correct result with this:
<?php $useruserBIG = 'emDSDla.ccount#gmail.com' ?>
<?php $resultsmall = strtolower($useruserBIG); ?>
<?php
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '', $resultsmall);
?>
<?php echo substr($newstr, 0, 8) ?>
result from above code:
emdsdlac
The problem is, I cannot display in my script result some like this:
<?php echo substr($newstr, 0, 8) ?>
I can display result only with variable some like:
<?php $resultaftertruncate ?>
can anyone else help me to customize this code?

how to use variable content as a part of a other variable name?

i want to use a variable content (value) as a part of a variable name before the output
this is my code in other file:
<?php $vidooid1="fk16zv6mw2jj"; ?>
<?php $vidooid2="eztx3n90w8xs"; ?>
<?php $vidooid3="eztx3n90w8xs"; ?>
<?php $vidooid4="eztx3n90w8xs"; ?>
<?php $vidooid5="eztx3n90w8xs"; ?>
<?php $vidooid6="eztx3n90w8xs"; ?>
<?php $vidooid7="eztx3n90w8xs"; ?>
<?php $vidooid8="eztx3n90w8xs"; ?>
<?php $vidooid9="eztx3n90w8xs"; ?>
<?php $vidooid10="eztx3n90w8xs"; ?>
and this is the code in the actual file:
<?php $titlenumber="1"; ?>
<?php $vidoo="https://vidoo.tv/e/"; ?>
and this is the echo final result:
<?php echo $vidoo; ?><?php echo $vidooid; ?>
I want that when I change the value that is inside the $titlenumber then that it is also changed in number that is next to the $videoid <----just here (i want to rename the string by using titlenumber)
This would be far better implemented using arrays.
e.g.
<?php
$vidoos = array(
"fk16zv6mw2jj",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs"
);
$titlenumber = 0;
$vidoo="https://vidoo.tv/e/";
echo $vidoo.$vidoos[$titlenumber];
$titlenumber = 3;
echo "<br/>"; //line break, just for demo
echo $vidoo.$vidoos[$titlenumber];
?>
Note that arrays start their indexes at 0 by default, so if you can start titlenumber at 0 too, you can do it simply like I've shown. If not, shift the array as shown in this answer
BTW you don't have to write a new <?php on every line. Just write it once at the start of the section of PHP, and close it once with ?> at the end. Lines in between only need to end with ;.
You can use concatenation as the following example shows:
$iterator = 1;
$variableName = 'vidooid';
$vidooid1 = 'some value';
echo ${$variableName . $iterator}; // outputs 'some value'
As the others said before, you should learn how to use arrays with PHP. The above shown solution is okay, but should not be used when you can use arrays instead.

How to display the first character from a WordPress title

In WordPress we use the code <?php the_title(); ?> for displaying the title of a post. Now, I want to display only the first letter of the title in a different place. How can I do that?
I have tried this, but it doesn't work:
<?php $my_title = the_title(); ?>
<?php
$first_char = $my_title[0];
echo $first_char;
?>
// Get the first character.
// $firstCharacter = $string[0];
$my_title = get_the_title();
// Get the first character using substr.
$firstCharacter = substr($my_title, 0, 1);
echo $firstCharacter;
The the_title() function will print it by default if the echo parameter is not set to false. get_the_title() will retrieve the title.
You can simply do this:
<?php echo get_the_title()[0]; ?>
As long as the title is not empty. Or:
<?php echo substr(get_the_title(),0,1); ?>
<?php echo preg_replace('/^(\w).+/','\1',get_the_title()); ?>
<?php echo str_split(get_the_title())[0]; ?>
<?php printf("%.1s", get_the_title()); ?> //echo sprintf
etc...
Or if you want to get complicated, you can use a "stream" yea!:
$f = fopen('php://memory', 'w+');
fputs($f, get_the_title());
rewind($f);
echo fgetc($f);
fclose($f);
LOL - that was the hardest way I could think of, that does what it's supposed to and doesn't have any unnecessary steps (well, except fclose, but in this case we can recover the memory);
Sandbox

strtolower and similar functions with wordpress 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)); ?>

adding a WHERE clause to this statement

I am looking to add a where clause to the following:
<?php foreach($this->assignments as $assignmentIndex => $assignmentArray): ?>
I want basically to only pull data from the database where assignmentType=pdf
any help would be great!
thanks
<?php foreach($this->assignments as $assignmentIndex => $assignmentArray): ?>
<?php if(assignmentType == 'pdf') :?>
....
....
<?php endif; ?>
<?php end foreach; ?>
well you can't change the sql line in the foreachloop
however if the assignmentArray containts assignmenType
you could ignore the other types like this:
if($assignmentArray['assignmentType'] != 'pdf'){
continue;
}
or somewhat nicer would be:
if($assignmentArray['assignmentType'] == 'pdf'){
// do your fancy stuff here
}

Categories