Merge two variables - php

How can I merge two variables?
My code:
$cases = $message->case_number;
$messageText = $message->password;
if (!empty($message->template)) {
$cases = str_replace('_CASE_', $cases, $message->template->text);
$messageText = str_replace('_MESSAGE_', $messageText, $message->template->text);
}
How can I merge $cases and $messageText?
Edit for clarity, from comments below
I want to replace $messageText with MESSAGE and $cases with CASE in just 1 variable something like
$test= str_replace('CASE', $cases, 'MESSAGE',$messageText, $message->template->text);

You can pass both replacement items in to array_replace() at the same time using an array...
if (!empty($message->template)) {
$output = str_replace(['_CASE_', '_MESSAGE_'],
[$cases, $messageText],
$message->template->text);
}

Use the dot to concatenate strings, like this:
$firstname = 'Dave';
$middlename = 'Brexit';
$lastname = 'Davis';
$fullname = $firstname . ' ' . $middlename . ' ' . $lastname; // 'Dave Brexit Davis'

You can always merge two or more variables together by just putting dot between them.
$cases = $message->case_number;
$messageText = $message->password;
if (!empty($message->template)) {
$cases = str_replace('_CASE_', $cases, $message->template->text);
$messageText = str_replace('_MESSAGE_', $messageText, $message->template->text);
$merged = $cases.$messageText;
}

Related

Create string variable to hold the first word of each array element

Essentially I have an array:
$names = array("firstName1 lastName1", "firstName2 lastName2", "firstName3 lastName3");
I want to create a string variable to hold firstName1, firstName2 and firstName3. Ideally the string should look like:
$firstNames = "firstName1, firstName2 & firstName3";
So I can create a script that looks like:
echo 'Thank you for your booking for'.$firstNames.' Your booking is now complete.'
I've tried looking at string concatenation via imploding with a 'for' loop, but didn't manage it. The array is not a fixed length, so I need the last element to be added with a " &".
Thanks for any help.
EDIT:
Using version 5.5.12, I have input:
$names = array("firstName1 lastName1", "firstName2 lastName2", "firstName3 lastName3");
$firstNames = '';
$finalFirstName = array_pop($names);
foreach ($names as $name)
{
$firstNames = $firstNames . ' ' . reset(explode(' ', $name));
}
$firstNames = ' & ' . explode(' ', $finalFirstName)[0];
echo $firstNames;
And my output is:
Strict standards: Only variables should be passed by reference on line 8.
But I am seeing text of:
& firstName3
I would use foreach instead of for loops for arrays with unknown length.
$firstNames = '';
foreach ($names as $name)
{
$firstNames = $firstNames . ' ' . reset(explode(' ', $name));
}
To put & before the last name, I would pop off the last name and appending it in the way you need it to be, turning the code into.
$firstNames = '';
$finalFirstName = array_pop($names);
foreach ($names as $name)
{
$firstNames = $firstNames . ' ' . reset(explode(' ', $name));
}
$firstNames = ' & ' . reset(explode(' ', $finalFirstName));
$tmp = array_map(
function ($value) { return explode(' ', $value)[0]; },
$names);
$firstNames = implode(', ', array_slice($tmp,0,count($tmp)-1)) .
" & " . end($tmp);
// firstName1, firstName2 & firstName3

php foreach not working properly with explode

Ok some php code below.
$user_pass = "
vortex90:OPFY4MB8
jimmy3:3M7ISWof
dave-ish-mental:YEnMMXua
cindybaby:rRHxrErp
claire-x:H4VrT8Xx
icemonster:ODId9N17
";
$token = 'token';
$ex = explode("\r", $user_pass);
foreach ($ex as $info) {
print "username=" . str_replace(":", "&password=", $info) . "&token=" . $token . "\n";
}
What i want the foreach() to do is show for each explode
username=username&password=password&token=token
But below is what gets returned.
vortex90&password=OPFY4MB8
jimmy3&password=3M7ISWof
dave-ish-mental&password=YEnMMXua
cindybaby&password=rRHxrErp
claire-x&password=H4VrT8Xx
icemonster&password=ODId9N17
Why is it not returning as expected? all answers welcome.
This works for me, it is better practice to use PHP_EOL:
$token = "bla";
$user_pass = "
vortex90:OPFY4MB8
jimmy3:3M7ISWof
dave-ish-mental:YEnMMXua
cindybaby:rRHxrErp
claire-x:H4VrT8Xx
icemonster:ODId9N17
";
$explode = explode(PHP_EOL, $user_pass);
foreach($explode as $i) {
$replace_shit = str_replace(array("\r","\n",":"), array("","","&password="), $i);
$user_info = "username=".$replace_shit."&token=".$token."<br>\n";
echo $user_info;
}
DEMO: http://sandbox.onlinephpfunctions.com/code/02f6663f7fa69c158a90fde2ab421cf52a78f7ce

Joining two arrays and echoing the results

I have 3 arrays $personal1 , $personal2 and $business , each one holds 1 field from each query,
so for example 'Mr' 'John Smith' 'Johnscorp Ltd'.
I am trying to construct the following if query >>
$personal2 = $userinfo->leadname;
$personal1 = $userinfo->salutation;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal1;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
What it does is check to see if the salutation is blank or not, if the field is blank the business name is echo'd instead.
The problem I have is how do I merge the personal and personal2 into one array ?.
I am not sure if I can do this :
$personal2 = $userinfo->leadname;
$personal1 = $userinfo->salutation;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal1 & $personal2;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
or if I can do this ?
$personal = $userinfo->salutation,$userinfo->leadname;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
or if both are incorrect as I dont seem to be getting any results :-S .
I think the first example you have should be working, just replace
$name = $personal1 & $personal2;
with
$name = $personal1 . ' ' . $personal2;
this will join these string with a space between them
PS: Didn't you think strings instead of arrays ?
I'm a bit puzzled because you've stated multiple times that you're using arrays, however in your example, it appears that you're using objects.
Anyways, if all you need is to merge $personal1 and $personal2,
instead of using:
$name = $personal1 & $personal2;
you can just use:
$name = $personal1 + $personal2;
If it's a string you need:
$name = $personal1 . ' ' . $personal2;
array_merge()
or just the + operator

Split Comma Separated Names with PHP

I have names in the form of Lastname, Firstname. In my database I have a different field for both the first and last.
I would like to use PHP to read everything before the comma as the lastname and everything after the comma as the firstname. What is the best way to accomplish this?
list($Lastname,$Firstname) = explode(",",$Name);
<?php
$names = explode( "," , $allNames);
// $names[0] and names[1] are first and last names
?>
with the explode function.
<?php
list($firstname, $lastname) = explode(',','Lastname, Firstname',2);
echo $firstname.' '.$lastname;
?>
If you'll use list();
while( list($fname,$lname) = explode(", ", $db->fetch() ) ) {
echo $lname . " " . $fname . "<br />";
}
Without list() and assining an array;
$name = explode( ", ", $db->fetch()->nameField );
// may be you want to do something with that array
// do something
// echoing
foreach( $name as $fname=>$lname ) {
echo $lname . " " . $fname . "<br />"
}
As nobody has mentioned it yet, to expressly meet the question requirements, you'll need to use the third parameter to explode()
list($lastname, $firstname) = explode(',', $name, 2);

How can I break an email address into different parts in php?

Basically what I want to do is display an email using javascript to bring the parts together and form a complete email address that cannot be visible by email harvesters.
I would like to take an email address eg info#thiscompany.com and break it to:
$variable1 = "info";
$variable2 = "thiscompany.com";
All this done in PHP.
Regards,
JB
list($variable1, $variable2) = explode('#','info#thiscompany.com');
$parts = explode("#", $email_address);
Assuming that $email_address = 'info#thiscompany.com' then $parts[0] == 'info' and $parts[1] == 'thiscompany.com'
You can use explode:
$email = 'info#thiscompany.com';
$arr = explode('#',$email);
$part1 = $arr[0]; // info
$part2 = $arr[1]; // thiscompany.com
$email = "info#thiscompany.com";
$parts = explode("#", $email);
Try this one before you roll your own (it does a lot more):
function hide_email($email)
{ $character_set = '+-.0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set); $cipher_text = ''; $id = 'e'.rand(1,999999999);
for ($i=0;$i<strlen($email);$i+=1) $cipher_text.= $key[strpos($character_set,$email[$i])];
$script = 'var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";';
$script.= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
$script.= 'document.getElementById("'.$id.'").innerHTML=""+d+""';
$script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")";
$script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>';
return '<span id="'.$id.'">[javascript protected email address]</span>'.$script;
}
How about a function for parsing strings according to a given format: sscanf. For example:
sscanf('info#thiscompany.com', '%[^#]#%s', $variable1, $variable2);

Categories