PHP replace string does not work - php

In my db a have this message MSG01:
"Availability between #DATA_MIN# - #DATA_MIN#"
Query:
select * from messages where MSG_CODE = "MSG01"
Column format is VARCHAR(500).
I have this code to replace #DATA_MIN# and #DATA-MAX#:
$date = array($date->data_min, $date->data_max);
$replace_string = array("#DATA_MIN#", "#DATA_MAX#");
$text= str_replace($replace_string,$date, lang("MSG01")).
But on my site it appears like this: Availability between #DATA_MIN# - #DATA_MIN#. Why does it not replace the values?

You have one of two problems:
Your site is not using $text from this example.
Your $date->data_min property has the value "#DATA_MIN#".

I've solved the problem. I've removed the # from DATA_MIN - DATA_MAX. It's working now.
Thank you, guys!

Related

converting date value from html form with dashes in wrong order to slashes in correct order

I have an html form that I am posting a date value in the following format:
2019-12-13
What I need (due to the software it is being imported into) is to format it like:
12/13/2019
what I tried:
$dateUSD = date($_POST['dateUSA'])->format('dd/mm/yyyy');
it did not work..
also trying to just change the - to /:
$dateUSD = $_POST['dateUSA'];
//2019-12-12
$dateUSD = str_replace('-', '/', $dateUSD);
But htat is not working either.
Just try this :
$date="2019-12-13";
echo date('d/m/Y',strtotime($date));
Output :
13/12/2019
Try this :
$date=date_create("2019-12-13");
echo date_format($date,"d/m/Y");
Result :
13/12/2019

Show names separated (Getting names from database)

In my database I have one column named name and in each row of that column I'm saving names like this /Mary/Sam/Bob/Michael/. To show the values in my page I need to separate them by breaking the line in each /. Can someone help me?
If I make one echo of my column I will get /Mary/Sam/Bob/Michael/ but I want:
Mary
Sam
Bob
Michael
$names=explode('/', $dbrow['name']);
foreach($names as $aname){
echo $aname.'<br>';
}
Use the explode function with array_filter function
$database_column_string = "/Mary/Sam/Bob/Michael/";
$names = array_filter(explode('/',trim($database_column_string )));
foreach($names as $name){
echo $name;
echo "<br/>";
}
\n may not render a new line in the web browser. So it's better to replace \ with the br tag:
str_replace ("/" , "<br>" , "/Mary/Sam/Bob/Michael/");
The explode function will fix your problem but you should be aware that the way you're using your database is violating the first normal form. Each record should have the same number of attributes.
Here is a guide to the normal forms http://www.bkent.net/Doc/simple5.htm
You should redesign your database to have a table called, for example, Names(id, name)

Add method return values together

I'm working on a project that uses the Facebook API. Im using methods from that API to automaticly create a username. But for that I need to add the firstname and the lastname.
The way on how I retrieve the firstname and lastnames is:
$userNode->getLastName();
$userNode->getFirstName();
How do I add those return values togheter?
If I do this:
$string = $userNode->getFirstName() + $userNode->getLastName();
echo "$string";
the echo shows "0".
Thanks in advance!
Mats de Waard
The first string operators example in the manual will show you:
$string = $userNode->getFirstName() . $userNode->getLastName();
You need to make a concatenation in php
in your example use "." instead of "+"
$string = $userNode->getFirstName() . $userNode->getLastName();

Short String after php explode

HI I get a string in our data base after completing a quiz which contains the results of each question in a single string. like:
&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3
each question can be skipped for a while to answer later, so the answers are unsorted.
now I want the results like
q1=2,q2=5,q4=9,q6=8,q12=1,q14=7,q19=10,q20=3
Can anyone help me.?
Try this
$a='&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
$b=explode('&',$a);
natsort($b);
$c=implode(',',$b);
print($c);
try like this
$a='&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
$a = ltrim($a,'&');
$b=explode('&',$a);
natsort($b);
echo $c=implode(',',$b);
try this one:
$url = '&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
parse_str($url, $urlDecoded);
$urlDecoded = array_flip($urlDecoded);
natsort($urlDecoded);
$urlDecoded = array_flip($urlDecoded);
var_dump($urlDecoded);
Other examples were fine but they all had a leading comma ,.
Here is an improved version:
$a=explode('&','&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3');
natsort($a);
echo substr(implode(',',$a),1);

url_title codeigniter showing underscores instead of hyphens

Hi I am trying to insert my article title in database in the following format this-is-a-new-title for the input This is a new Title. For this I have written :
$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title,'-',TRUE);
But the echo $topic_slug_title shows titles like this_is_a_new_title. Why the underscores are added wherein I have given hyphens ?
Ok I found the solution. Just leave the second parameter as default,add the third parameter. That is:
$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title,TRUE,TRUE);
don't use the third parameter, use it like this
$title = $this->input->post('topic_title');
$topic_slug_title = strtolower(url_title($title));
don't use second and third parameter, write like this:
**$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title);**

Categories