How to use Wordpress Trim Words Function - wordpress

i want to trim a single word i.e, Monday in wordpress, how can i trim this word?
$my_title = get_the_title();
echo wp_trim_words($my_title, 1, null );
the title coming from database Monday So i want to trim Monday to Mo or M.

wp_trim_words() will works easily without any problems. You can use it with below sample code:
echo wp_trim_words( get_the_title(), 1, '' );
But do note that it will trim the first character in whole post/page title, not regconize the date in your example.

I don't know that wp_trim_words() works that way. I'd recommend just using regex and preg_match():
$my_title = get_the_title();
to get the first character:
$regex = '/(.?)/';
or to get the first two characters, change the variable to this:
$regex = '/(.?)./';
Then get the matches for the regex pattern:
preg_match($regex, $my_title, $matches);
Echo out the first one:
echo $matches[0];

Related

echo number format round

in word press I am trying to echo price like round last 3 digit.
Example
$price= 15.786.625;
I whant after echo price like this
price=15.786.000
How I can do it?
I find a way do it this.
Say
$price=4,316,028
$pricecarp=round($price/1000)*1000;
<?php
echo number_format($pricecarp, 0, '.', ',');
?>
Result= 4,316,000

PHP's explode() is not working on hyphen-minus -

I am try to use explode() on a string fetched from a database but it didn't work. I have tried explode('-',$string) but it's still not working.
Here is my string which I want to explode:
Expression of Interest – Join our Paint Team – North
If you look closely the hyphen from the string is not the same as the hyphen you use as your argument for the explode.
The hyphen in the string is the following – while the hyphen you pass as the argument for explode() is -. As you can see they don't match (the one in the string is longer than the one you try to compare it with). Because the characters don't match, the explode function is returning the whole string.
<?php
$string = "Expression of Interest – Join our Paint Team – North";
$strings = explode('–', $string);
var_dump($strings);
I have copied the hyphen from the text and used that as an argument for explode() and it works fine.
Might be $string is not string, you can use strval( $string ) to convert it to string i.e. explode('–', strval ( $string ) );
$eString = explode('–', strval ( $string ) );
// vardump($eString) - Now it is an array.
// echo $eString[0];
I have fix the issue by trying this
$post_job_title = htmlentities(get_the_title($posts));
$post_job_title = explode (" – ", $post_job_title);

How to format an already formatted number to another number format?

I have already number formatted value. It is formatted as:
$val = number_format($val,'2', '.', ',');
$val = hook_price_filter($val);
It is a Wordpress core theme code. It provides a price hook.
So, I want to format it to another number format as number_format($val,'2', ',', '.') e.g. from 2,152.14 to 2.152,14
Please help me on this.
Remove the commas, because number_format wants a float number as first parameter, not a string:
number_format( str_replace(',', '', $val), 2, ',', '.' );

php capitalize second letter in sentence min 10 characters long

Hello I have a project where I need to cApitalize only the second letter in a sentence. I now that PHP has strtoupper() and string strtoupper ( string $string ) ucfirst() returns first letter
So here is my best attempt
<?php
$str = "capitalize";
$str = ucfirst(strtolower($str)); // makes all the letters lower case
?>
This is where I get confused if 0 = the first letter and 1= 2nd then could I just make an array(") or count_chars() then $val
Its a old question, just came across this so would put an answer based on #doppelgreener comment.
This should work :
$str = "capitalize";
$str[1]= strtoupper($str[1]);
echo $str; // cApitalize
i have one idea to perform this operation..
example
$strmain='capitalize';
$result = substr($strmain, 0, 1); //result is c
$result1=str_replace($result,'',$strmain);//now your result1 is apitalize
$result2=ucfirst($result1); //now result2 is Apitalize
$finalresult=$result.$result2 ///now your finalresult is cApitalize

mysql_fetch_row loop?

I set a session variable upon login, I want to find all rows in a table that have the username in the "Createdby" field and I want to list them on a page. I'm using this code:
<?php
$result = mysql_query("SELECT email FROM members WHERE createdby = '" . $_SESSION['myusername'] ."'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_row($result))
{
echo ($row[0]);
}
?>
It works great but it doesn't space them, it echoes out like: data1data2 and not separate like data1, data2. How can I customize the results without messing it up? I tried to add
echo ("<p>".$row[0]."</p>");
But received: 11, I'm kind of new to PHP.
This is simply string concatenation. You're adding strings to other strings.
I'd suggest the following just to get started:
echo $row[0].", ";
I also suggest you read up on PHP more, as this is a basic concept in the language.
You can add spaces between each $row[0] like this:
echo $row[0] . ' ';
The dot followed by a space means that you're concatenating a space.

Resources